How to Dockerize an ExpressJS App ?
Last Updated :
07 Mar, 2025
Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its software, libraries, and configuration files. This article will discuss how to dockerize an Express app for the development and deployment phases.
Prerequisites: Before continuing any further, please ensure that node and docker are installed on your machine. If required visit Node Installation Guide or Docker Installation Guide.
Dockerizing an Express application:
For this tutorial, we will create a simple Node.js Express application, which will act as a REST API.
Step 1: Initialize an empty node project by running the following commands:
$ mkdir express-docker
$ cd express-docker
$ npm init -y
Step 2: Install express and create a new file index.js:
$ npm install express
$ touch index.js
Step 3: Paste the following into index.js:
JavaScript
const express = require('express'),
app = express();
app.use(express.urlencoded({ extended: true }))
app.use(express.json())
app.get('/',
(req, res) => res.send('Dockerizing Node Application'))
app.listen(5000,
() => console.log(`⚡️[bootup]: Server is running at port: 5000`));
Project Structure: This is how the project structure should look at this point.
Step 4: Start the server by running:
$ node index.js
Note: Now, if you go to the URL http://localhost:5000/ you'll see the below output:
Dockerfile for development: At the root of our react project create a Dockerfile for the development phase. Let's name it Dockerfile.dev.
$ touch Dockerfile.dev
In the newly created file, paste the following commands:
# Fetching the minified node image on alpine linux
FROM node:slim
# Declaring env
ENV NODE_ENV development
# Setting up the work directory
WORKDIR /express-docker
# Copying all the files in our project
COPY . .
# Installing dependencies
RUN npm install
# Starting our application
CMD [ "node", "index.js" ]
# Exposing server port
EXPOSE 5000
Create a .dockerignore file to exclude unnecessary files thus speeding up the build process.
node_modules
npm-debug.log
build
.git
*.md
.gitignore
Now create a docker image by using the docker build command
$ docker build -f Dockerfile.dev -t [name:tag] .
Here,
- -f: Path to the docker file
- -t: Name and tag for the image
- . : The context for the build process
Once the build process has been completed, you will receive the id and tag of your new image.
Create a docker container by running:
$ docker run -d -it –-rm -p [host_port]:[container_port] --name [container_name] [image_id/image_tag]
Here,
- -d: Run the container while printing the container ID.
- -it: Create an interactive container
- -p: Map host port to container port
- --name: Assign a name to the container
- --rm: When it leaves, take the container away automatically.
Verify whether the container has been created successfully by running:
$ docker container ps
Navigate to http://localhost:5000/ to verify the build process.
Dockerfile for production: The deployed application will crash or stop working when there is any unhandled exception or error in our server and we have to manually restart it by interacting with our docker container via the command-line interface. So, for the final step will install a process manager that will automatically restart the server and ensure everything runs smoothly in production. This is where PM2 comes into the picture. PM2 is an open-source, cross-platform production-level process manager that comes with a built-in load balancer.
Create a YAML file for PM2 at the root of our express application:
$ touch process.yml
Paste the following content into the process.yml file:
Ruby
apps:
- script: index.js
instances: 4
exec_mode: cluster
Here, we are telling the PM2 to start four instances of our server in cluster mode.
Next, create a new Dockerfile for production mode:
$ touch Dockerfile
Paste the following commands:
# Fetching the minified node image on apline linux
FROM node:slim
# Declaring env
ENV NODE_ENV production
# Setting up the work directory
WORKDIR /express-docker
# Copying all the files in our project
COPY . .
# Installing dependencies
RUN npm install
# Installing pm2 globally
RUN npm install pm2 -g
# Starting our application
CMD pm2 start process.yml && tail -f /dev/null
# Exposing server port
EXPOSE 5000
Repeat the same steps to build an image from the updated Dockerfile and create a container out of it.
$ docker build -t [name:tag] .
$ docker run -d -it -–rm -p [host_port]:[container_port] –name [container_name] [image_id/image_tag]
Verify whether the container has been created successfully by running:
$ docker container ps
Project Structure: This is how the project structure should look at this point:
Open your browser and navigate to http://localhost:5000/ in your browser to view the dockerized express app.
Note: Please refer to the Dockerfile used in this tutorial if you are having any difficulties following the above steps.
Similar Reads
Docker Tutorial Docker is a tool that simplifies the process of developing, packaging, and deploying applications. By using containers, Docker allows you to create lightweight, self-contained environments that run consistently on any system, minimising the time between writing code and deploying it into production.
7 min read
Introduction
Docker Installation
Docker - Installation on WindowsIn this article, we are going to see how to install Docker on Windows. On windows if you are not using operating system Windows 10 Pro then you will have to install our docker toolbox and here docker will be running inside a virtual machine and then we will interact with docker with a docker client
2 min read
How to Install Docker using Chocolatey on Windows?Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Commands
Docker CommandsDocker is an open-source project that automates the deployment of applications as movable, independent containers that can run locally or in the cloud. You can divide your applications from your infrastructure with the help of Docker, allowing for quick software delivery and it also allows you to ma
7 min read
How to Install Docker using Chocolatey on Windows?Installing Docker in Windows with just the CLI is quite easier than you would expect. It just requires a few commands. This article assumes you have chocolatey installed on your respective windows machine. If not, you can install chocolatey from here. Chocolatey is a package manager for the Windows
4 min read
How to Install and Configure Docker in Ubuntu?Docker is a platform and service-based product that uses OS-level virtualization to deliver software in packages known as containers. Containers are separated from one another and bundle their software, libraries, and configuration files. Docker is written in the Go language. Docker can be installed
6 min read
How to Install Docker on MacOS?Pre-requisites: Docker-Desktop Docker Desktop is a native desktop application for Windows and Mac's users created by Docker. It is the most convenient way to launch, build, debug, and test containerized apps. Docker Desktop includes significant and helpful features such as quick edit-test cycles, fi
2 min read
How to install and configure Docker on Arch-based Linux Distributions(Manjaro) ?In this article, we are going to see how to install and configure Docker on Arch-based Linux Distributions. Docker is an open-source containerization platform used for building, running, and managing applications in an isolated environment. A container is isolated from another and bundles its softwa
2 min read
How to Install Docker-CE in Redhat 8?Docker is a tool designed to make it easier to create, deploy, and run applications by using containers. Containers allow a developer to package up an application with all the parts it needs, such as libraries and other dependencies, and deploy it as one package. Installing Docker-CE in Redhat 8: St
2 min read
Docker Commands
Docker Images
What is Docker Image?Docker Image is an executable package of software that includes everything needed to run an application. This image informs how a container should instantiate, determining which software components will run and how. Docker Container is a virtual environment that bundles application code with all the
10 min read
Working with Docker ImagesIf you are a Docker developer, you might have noticed that working with multiple Docker Images at the same time might be quite overwhelming sometimes. Managing numerous Docker Images all through a single command line is a very hefty task and consumes a lot of time. In this article, we are going to d
2 min read
Docker - Publishing Images to Docker HubDocker is a container platform that facilitates creating and managing containers. In this article, we will see how docker stores the docker images in some popular registries like Dockerhub and how to publish the Docker images to Docker Hub. By publishing the images to the docker hub and making it pu
8 min read
Docker CommitDocker is an open-source container management service and one of the most popular tools of DevOps which is being popular among the deployment team. Docker is mostly used in Agile-based projects which require continuous delivery of the software. The founder, Chief Technical Officer, and Chief Archite
10 min read
Docker - Using Image TagsImage tags are used to describe an image using simple labels and aliases. Tags can be the version of the project, features of the Image, or simply your name, pretty much anything that can describe the Image. It helps you manage the project's version and lets you keep track of the overall development
7 min read
Next.js Docker ImagesUsing Next.js Docker images allows your app to deploy to multiple environments, and is more portable, isolated and scalable in dev and prod. Dockerâs containerization makes app management super easy, you can move from one stage to another with performance.Before we get started, letâs cover the basic
14 min read
How to Use Local Docker Images With Minikube?Minikube is a software that helps in the quick setup of a single-node Kubernetes cluster. It supports a Virtual Machine (VM) that runs over a docker container and creates a Kubernetes environment. Now minikube itself acts as an isolated container environment apart from the local docker environment,
7 min read
Docker Compose
Docker Engine, Storage
Docker Networking
Docker NetworkingPre-requisite: Docker Docker Networking allows you to create a Network of Docker Containers managed by a master node called the manager. Containers inside the Docker Network can talk to each other by sharing packets of information. In this article, we will discuss some basic commands that would help
5 min read
Docker - Managing PortsPre-requisites: Docker Docker is a set of platform-as-a-service products that use OS-level virtualization to deliver software in packages called containers. These containers may need to talk to each other or to services outside docker, for this we not only need to run the image but also expose the c
4 min read
Creating a Network in Docker and Connecting a Container to That NetworkNetworks are created so that the devices which are inside that network can connect to each other and transfer of files can take place. In docker also we can create a network and can create a container and connect to the respective network and two containers that are connected to the same network can
2 min read
Connecting Two Docker Containers Over the Same NetworkWhenever we expose a container's port in docker, it creates a network path from the outside of that machine, through the networking layer, and enters that container. In this way, other containers can connect to it by going out to the host, turning around, and coming back in along that path.Docker of
3 min read
How to use Docker Default Bridge Networking?Docker allows you to create dedicated channels between multiple Docker Containers to create a network of Containers that can share files and other resources. This is called Docker Networking. You can create Docker Networks with various kinds of Network Drivers which include Bridge drivers, McVLAN dr
7 min read
Create your own secure Home Network using Pi-hole and DockerPi-hole is a Linux based web application, which is used as a shield from the unwanted advertisement in your network and also block the internet tracking system. This is very simple to use and best for home and small office networks. This is totally free and open-source. It also allows you to manage
3 min read
Docker Registry