How-Tos

Enhance Productivity with Make: A How-To Guide

Using Make and Makefile for Docker Management in a NodeJS Application

1* uN otMpuas3lCD6h6lnZQ

Make is a powerful build automation tool commonly used to manage the build process of software projects. It uses a file called Makefile to define a set of tasks to be executed. In this blog, we’ll explore how to use Make and Makefile to manage Docker operations for a NodeJS application.

Prerequisites

Before we dive in, ensure you have the following installed on your machine:

  • Docker
  • Make — MacOS | Windows
  • NodeJS (For this, I am using NestJS Framework with Nest CLI)

Setting Up the Project

First, let’s set up a basic NestJS application. Open your terminal and run the following commands:

npm i -g @nestjs/cli
nest new makefile-demo-app
cd makefile-demo-app

This will create a new NestJS application in the makefile-demo-app directory.

Dockerizing the Application

Next, we’ll create a Dockerfile to containerize our NestJS application. Create a file named Dockerfile in the root of your project with the following content:

# Use an official Node.js runtime as a parent image
FROM node:16-alpine

# Set the working directory in the container
WORKDIR /app

# Copy the package.json and package-lock.json files to the container
COPY package*.json ./

# Install any needed packages specified in package.json
RUN npm install

# Copy the current directory contents into the container at /app
COPY . .

# Make port 80 available to the world outside this container
EXPOSE 3000

# Run the application when the container launches
CMD ["npm", "start"]

Creating the Makefile

Now, let’s create a Makefile to manage our Docker operations. Create a file named Makefile in the root of your project.

Now, we will develop targets in the Makefile.

Example 1: Build an Docker image

build_docker:
docker build -t makefile-demo-app .

Example 2: Run an Docker image

run_docker:
docker run --name makefile-demo-app -p 3000:3000 -d makefile-demo-app

Example 3: Create a Target which is dependent on another target

build_and_run: build_docker run_docker

In this way, we can create as many targets as we want.

Complete Makefile

build_docker:
docker build -t makefile-demo-app .

run_docker:
docker run --name makefile-demo-app -p 3000:3000 -d makefile-demo-app

stop_docker:
docker stop makefile-demo-app

delete_image:
docker rm makefile-demo-app

build_and_run: build_docker run_docker

stop_and_delete: stop_docker delete_image

Explanation of Makefile Targets

  • build_docker: This target builds the Docker image for our NestJS application using the Dockerfile.
  • run_docker: This target runs the Docker container from the built image, mapping port 3000 of the container to port 3000 on the host.
  • stop_docker: This target stops the running Docker container.
  • delete_image: This target removes the Docker container.
  • build_and_run: This target combines build_docker and run_docker to build the image and run the container in one command.
  • stop_and_delete: This target combines stop_docker and delete_image to stop and remove the container in one command.

Using the Makefile

To use the Makefile, open your terminal and navigate to the root of your project. You can then run the following commands:

  • Build the Docker image:
make build_docker
  • Run the Docker container:
make run_docker
  • Build and run the Docker container:
make build_and_run

Makefile provides a simple interface for executing complex commands. As in this example, you can use short and intuitive Make targets instead of remembering and typing long Docker commands.


Makefile rules/targets

  • A rule/target consists of three parts: one or more targets, zero or more dependencies, and zero or more commands in the form:
target: dependencies
<tab> commands to make target
<tab>
• character MUST NOT be replaced be spaces.
• A "target" is usually the name of a file(e.g. executable
or object files). It can also be the name of an action.
• "dependencies" are files that are used as input to create
the target.
• Each "command" in a rule is interpreted by a shell to be executed.
• By default, make uses /bin/sh shell.
• Typing "make <rule/target>" will run the target commands.

Rajesh Mishra

I'm a developer who loves sharing insights, technical how-tos, and lessons learned from the world of code. While much of what I write may not be groundbreaking, I believe in documenting for future me—and for anyone else who might find it useful. Beyond tech, I also dive into life's experiences and moments, reflecting on personal growth and sharing stories that resonate. Whether you're here for practical tips or a fresh perspective on life, I hope you find something meaningful.

Leave a Reply

Your email address will not be published. Required fields are marked *