Dockerize an Express.js App



To dockerize an ExpressJS app, we will be going through a detailed stepwise explanation in this article. Containerization is becoming an essential in the software development process to ensure that the software is developed and deployed in different environments and in a consistent manner. Docker is the most popular containerization tool that helps in packaging an app and its dependencies.

Problem Statement

This article focuses on the challenges of deploying Node.js applications due to dependency conflicts, environmental issues, and system-specific settings. In the traditional way of deployment, the runtime environment has to be set up manually which resulted in the difference between the development, testing, and production environments.

Prerequisites

Before you start, make sure you have the following installed on your system:

  • Docker
  • Node.js (for local development)
  • Basic knowledge of JavaScript and Express.js

Steps to Dockerize an ExpressJS App

We will be following the steps mentioned below to dockerize an expressJS app.

Step 1: Setting Up the Express.js Application

First of all, let's create a simple Express application. Create a new project directory and navigate into it:

mkdir express-docker-app
cd express-docker-app

Initialize a new Node.js project:

npm init -y

Install Express.js:

npm install express

Create a file with the name server.js and the following content:

const express = require("express");
const app = express();
const PORT =  4000;
app.get('/', (req, res) => {  res.send('Welcome to your Dockerized Express.js app!'); });
app.listen(PORT, () => {  console.log(`Server is running on port ${PORT}`);  });

Step 2: The second step is to create the Dockerfile

Then, we have to define a Dockerfile that describes how to build our Docker image.

Create a file named Dockerfile in the project directory and add the following content:

# Use the official Node.js runtime as the base image.
FROM node:16

# Set the working directory inside the container
WORKDIR /usr/src/app

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

# Install dependencies
RUN npm install

# Copy the entire application source code into the container
COPY..

# Expose the application port
EXPOSE 4000

# Define the command  to run the application
CMD ["node", "server.js"]

Step 3: Building the Docker Image

When the Dockerfile is created, we can create a Docker image for the application. Open a terminal and navigate to the project directory and type:

docker build -t  express-docker-app.

This command creates a Docker image express-docker-app based on the Dockerfile.

Step 4: Docker Container Running

Now that the Docker image has been built, it is possible to run a container from it.

docker run -d -p  8080:4000 express-docker-app

This is what happens:

-d: Runs the container in the background (detached).

-p 8080 : 4000: Maps port 4000 inside the container to port 8080 on the host machine.

express-docker-app: The name of the Docker image to run.

Then, launch the browser and go to http://localhost:8080. You will see the welcome message "Welcome to your Dockerized Express.js app!".

Step 5: Docker Compose Usage for Simpler Deployment

We do not have to use the Docker build and run commands directly but can use Docker Compose to orchestrate our application in a more streamlined manner. Create a docker-compose.yml file and place it in the root directory of the project and add the following content:

Use the following command to start the application using Docker Compose:

docker-compose up -d

This command creates a build and run container in the detached mode for easy control.

Other Docker Commands

List Running Containers - To list all running containers:

docker ps

Stop a Running Container - To stop a specific container:

docker stop  express-docker-container

Delete a Container - To delete a stopped container:

docker rm  express-docker-container

Delete an Image -To delete an image:


docker rmi express-docker-app

Conclusion

When you Dockerize your Express application, you get the advantages of consistent environments, easy deployment, and enhanced scalability. Hence, the use of Docker or Docker Compose during containerization increases the efficiency in the management of the application. You should also extend this to include a database container, environment variables, or a multi-container application with Docker Compose.

Updated on: 2025-03-07T16:44:28+05:30

35 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements