Dockerizing a Node.js Application: A Step-by-Step Tutorial

Dockerizing a Node.js Application: A Step-by-Step Tutorial. In this guide, we explain whatNode.js is, benefits of Dockerizing a Node.js application. After that we show you how to Dockerize a Node.js application on Ubuntu Linux.

What is Node.js?

Node.js is a popular server side platform used by developers for building scalable network applications as well as writing server side code. Free and works well with different platforms. Some developers even use the framework to create dynamic web pages and apps for streaming video.

Ryan Dahl in 2009 released the framework to easily build fast and scalable web app development projects. It is basically a server environment powered by JavaScript that is open source, free to use, and customizable to the user’s requirements. The robust framework even offers a high throughput and scalability features in addition to storing JavaScript modules.

Ideal for data intensive real time applications that run across multiple devices because Node.js uses an event driven, non blocking I/O style that makes it lightweight and efficient. It implies that even while processing a large amount of data simultaneously, memory and CPU usage will be minimal. Additionally, for developers, Node.js is a simple choice because JavaScript is used to control both the front end and the back end of a programme.

In fact, some of the developers use the Node.js framework for building command line tools and running applications on Windows, Linux, and OS X platforms. Additionally, Node.js offers a comprehensive library of different JavaScript modules, which helps simplify the process of creating web applications with Node.js.

It even has an event loop, which enables developers to handle several requests at once and process them in whichever order they are received. It is one of the highly efficient frameworks for building fast and scalable real time applications.

Benefits of Dockerizing a Node.js Application

The V8 JavaScript engine in Chrome serves as the foundation for the open source Node.js runtime environment. Building server side and networking applications is made possible by the ability for developers to run JavaScript code outside of a web browser. Due to its event driven, non blocking I/O approach, Node.js is lightweight and effective at managing several concurrent requests.

If you want to have the same runtime environment during the development, testing, or production stage, it is best to dockerize your application. Dockerizing Node.js apps entails packaging and deploying Node.js programmes, together with their dependencies and configurations, within Docker containers. Here are a few benefits of Dockerizing your Node.js Applications:

1. Version Control – Version control is made possible for Node.js apps using Docker containers. The application’s various versions is easily tracked and managed thanks to the ability to distribute each container with a particular version. This makes rollback processes simpler and makes it easier to test and implement new features.

2. Highly Portable – Any machine with Docker installed may run a Node.js application once it has been Dockerized, independent of the underlying operating system. This facilitates the consistent deployment and operation of Node.js apps across several environments, such as development, staging, or production.

3. Continuous Integration and Deployment – Continuous Integration and Deployment pipelines work well with Docker. It is simpler to maintain a consistent and repeatable deployment pipeline by containerizing Node.js apps and automating all the deployment processes.

4. Easy Dependency Management – With Docker, list the application’s dependencies, such as the version of Node.js, libraries, and modules, in a Dockerfile. This prevents conflicts and incompatibilities by ensuring that everyone in the team is using the same dependency versions.

5. Scalability and Load Balancing – Running several instances of the containerized application using Docker enables you to scale Node.js applications horizontally. Utilizing effective load balancing and scaling based on demand is made possible by container orchestration solutions like Docker Swarm or Kubernetes, ensuring high availability and efficient resource utilization.

6. Consistent Development Environment – For Node.js application development, Docker offers a consistent environment on several machines, regardless of their local setup, operating system, or installed dependencies. Developers easily operate in the same environment by packaging the application and its dependencies into a container.

Dockerizing a Node.js Application: A Step-by-Step Tutorial

In this section, we show you how to Dockerize a Node.js application on Linux.


  • A server running Ubuntu Linux operating system.
  • A root user or a user with sudo privileges.

Update the System

First, I would recommended to update all your system packages to the latest version. Run the following commands to update and upgrade all the packages.

					apt update -y
apt upgrade -y

After updating all the packages, install other required dependencies using the following command.

					apt install apt-transport-https ca-certificates curl software-properties-common

Once all the packages are installed, proceed to install Docker.

Install Docker Package

Ubuntu default repository does not contains the latest Docker version. So you need to add the Docker’s official repository to get the latest Docker version.

First, add the Docker GPG key using the following command.

					curl -fsSL | apt-key add -

Then, add the Docker official repository with the following command.

					add-apt-repository "deb [arch=amd64] focal stable"

Once the repository is added to the APT. Install the Docker engine using the following command.

					apt install docker-ce -y

After the successful installation, verify the Docker service status with the following command.

					systemctl status docker

See the Docker status on the following screen.

Install Node.js

You also need to install the Node.js package to your system. To get the latest Node.js version, add the Node source repository to your system.

					curl -sL -o

Once the repository is added, install the latest Node.js using the following command.

					apt install nodejs -y

After the installation, verify the Node.js version with the following command.

					node -v

You see the Node.js version in the following output.


Create a Node.js Application

Now, you need to create a simple Node.js application in your server and test it. First, create a directory for your application using the following command.

					mkdir node-app

Next, navigate to the Ndoe application directory and initialize the Node.js with the following command.

					cd node-app
npm init -y

You see the following screen.

Next, install the Express using the NPM command.

					npm install express --save

Then, create a server.js file.

					nano server.js

Add the following code.

					// server.js

const express = require('express');

//Create an app
const app = express();
app.get('/', (req, res) => {
    res.send('This is a sample Node.js Application\n');

//Listen port
const PORT = 8080;
console.log(`Running on port ${PORT}`);


Save and close the file when you are done. Then, run your application with the following command.

					node server.js

Once the application started successfully, open your web browser and access your Node application using the URL http://your-server-ip:8000. You should see your application on the following screen.

Now, press the CTRL+C to stop the Node.js application.

Dockerize Node.js Application

At this point, Node.js application is created and tested on your server. Now, you need to copy all your application file inside the Docker container and build a image from it.

To do so, first, create a Dockerfile in your application root directory.

					nano Dockerfile

Add the following configurations.

					FROM node:18

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

CMD [ "node", "server.js" ]


Save and close the file after you finished. Then, create a dockerignore file and define all files that you want to exclude from Docker image.

					nano .dockerignore

Add the following line.


Save and close the file then create a Docker image called node-app with the following command.

					docker build -t node-app .

You should see the image build process on the following screen.

Now, verify your build image using the following command.

					docker images

You see the following output.

					REPOSITORY   TAG       IMAGE ID       CREATED          SIZE
node-app     latest    3a9b7b49bee9   35 seconds ago   1.1GB


Now, launch the Node application container using the node-app Docker image.

					docker run -dit --rm -p 8080:8080 --name node-container node-app

If everything is fine, you see the following output.


Check your container

Now check the running container with the following command.

					docker ps

This shows you the status of your node-app container.

Also check the real-time log of your running container with the following command.

					docker logs node-container

This shows you the following output.

					Running on port 8080

If you want to stop your node-app container, run the following command.

					docker container stop node-container

Remove the node-app image from your system using the following command.

					docker rmi node-app

Dockerizing a Node.js Application: A Step-by-Step Tutorial Conclusion

In this step by step guide, we explained how to install Node.js and Docker on Ubuntu Linux. Then, we created a Node.js application, dockerize it and run the Node.js application inside the container.  Now push your Node.js application image to the DockerHub registry and use it to deploy the Node.js application on any machine.

Overall, Dockerizing Node.js apps improve the workflows for development and deployment, making it simpler to construct, release, and manage Node.js applications in a repeatable and consistent manner.

Avatar for Hitesh Jethva
Hitesh Jethva

I am a fan of open source technology and have more than 10 years of experience working with Linux and Open Source technologies. I am one of the Linux technical writers for Cloud Infrastructure Services.

0 0 votes
Article Rating
Notify of
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x