DEV Community

Hossain Chisty
Hossain Chisty

Posted on • Edited on

Dockerizing a Node.js Application from Scratch

Image description

Dockerizing a Node.js application can greatly simplify the deployment process and improve the scalability of your application. Docker is a containerization platform that allows you to package your application and its dependencies into a container, which can then be run on any platform that supports Docker.

In this article, we'll go over the steps required to Dockerize a Node.js application from scratch.

Step 1: Create a Node.js Application
The first step is to create a Node.js application that we want to Dockerize. For this, we'll use the Express.js framework, which is a popular web application framework for Node.js.

To create an Express.js application, we first need to install Node.js and the npm package manager on our machine. Once we have Node.js and npm installed, we can create a new Express.js application by running the following command:

npx express-generator-esmodules myapp
Enter fullscreen mode Exit fullscreen mode

This will create a new Express.js application in a directory called myapp. Once the application is created, we can navigate into the directory and install its dependencies using npm:

cd myapp
npm install
Enter fullscreen mode Exit fullscreen mode

Step 2: Create a Dockerfile
The next step is to create a Dockerfile, which is a configuration file that describes how to build a Docker image for our Node.js application.

Create a new file called Dockerfile in the root directory of your application and add the following content:

# Use an official Node.js runtime as a parent image
FROM node:slim

# Set the working directory to /app
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of the application code to the working directory
COPY . .

# Expose port 3000
EXPOSE 3000

# Start the application
CMD [ "npm", "start" ]
Enter fullscreen mode Exit fullscreen mode

Let's go through this Dockerfile line by line:

  • FROM node:slim: This line specifies that we want to use the official Node.js runtime as the base image for our Docker image. Slim is a lightweight Linux distribution that's commonly used for Docker images because of its small size.
  • WORKDIR /app: This line sets the working directory for our Docker image to /app.
  • COPY package*.json ./: This line copies the package.json and package-lock.json files to the working directory.
  • RUN npm install: This line installs the dependencies listed in package.json.
  • COPY . .: This line copies the rest of the application code to the working directory.
  • EXPOSE 3000: This line exposes port 3000, which is the port that our Node.js application will listen on.
  • CMD [ "npm", "start" ]: This line specifies the command to run when the Docker container starts. In this case, we're starting our Node.js application using the npm start command.

Step 3: Build the Docker Image
Now that we have a Dockerfile, we can use it to build a Docker image for our Node.js application. To do this, we'll use the docker build command.

Open a terminal window, navigate to the root directory of your application, and run the following command:

docker build -t myapp .

This command tells Docker to build a new image using the Dockerfile in the current directory, and tag it with the name myapp. The . at the end of the command specifies the build context, which is the directory that Docker uses as the root of the build process.

Step 4: Run the Docker Container
Now that we have a Docker image for our Node.js application, we can use it to run a Docker container. To do this, we'll use the docker run command.

Open a terminal window and run the following command:

docker run -p 3000:3000 myapp

This command tells Docker to run a new container using the myapp image that we just built, and map port 3000 from the container to port 3000 on the host machine. This allows us to access our Node.js application running inside the container from our web browser.

If everything is set up correctly, you should see output in the terminal window indicating that your Node.js application is running. You can now open a web browser and navigate to http://localhost:3000 to see your application in action.

Conclusion
In this article, we went over the steps required to Dockerize a Node.js application from scratch. We started by creating a new Node.js application using the Express.js framework, then created a Dockerfile that described how to build a Docker image for our application. We then built the Docker image and ran a Docker container using that image. By following these steps, you should now have a fully Dockerized Node.js application that can be easily deployed and scaled.

Thank you for your time!

Top comments (3)

Collapse
 
webjose profile image
José Pablo Ramírez Vargas

A couple of points:

  1. We should all stop writing CommonJS and move to ES Modules. For Express applications, we should use express-generator-esmodules instead.
  2. When creating Docker images, it is best to use the npm ci command instead of npm install.

Cheers!

Collapse
 
hossainchisty profile image
Hossain Chisty

I am certainly grateful for your recommendations.

Cheers!

Collapse
 
webjose profile image
Info Comment hidden by post author - thread only accessible via permalink
José Pablo Ramírez Vargas

NP. Also what's up with the old Node image? There's Node v20 already. Your Dockerfile is pulling Node v14. :-P Get on board with new Node's!

Some comments have been hidden by the post's author - find out more