In this article, We are going to dockerize a Node JS application. Docker enables you to run applications in a containerized environment. Docker containers are isolated from each other and from the host operating system. This means that you can run multiple containers on a single computer and they will all have their own private copy of the application code. Docker containers also isolate your application from the host operating system, so you can run the same application code on multiple operating systems. In this tutorial, you'll learn the process of Dockerizing Node.js application from scratch. After reading this article, you should be armed with enough knowledge to Dockerize your own applications, even if they're built with some other technology.

Setting Up a Node.js Application

To continue with this tutorial, we will use simaple Node.JS application which output `Hello World`. You can clone its GitHub repository to your computer using the command below:
1
git clone https://github.com/dinushchathurya/dockerize-nodejs

Once downloaded, cd into the project folder. Afterward, open up the server.js file in your text editor. You should see the following content:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const express = require('express');

// Constants
const PORT = 3000;
const HOST = '0.0.0.0';

// App
const app = express();
app.get('/', (req, res) => {
res.send('Hello world\n');
});

app.listen(PORT, HOST);
console.log(`Running on http://${HOST}:${PORT}`);

Specifying 0.0.0.0 as the address is essential when deploying to Docker because Docker containers do not default to exposing mapped ports to ``localhost`. If this address is missing, your application might be inaccessible despite starting successfully in the container.

Installing Docker

Before you can Dockerize an application, you need to install the Docker Engine. The official Docker manual provides a guide for installing the software on a variety of operating systems, most notably on macOS, Windows, and a variety of Linux distributions. Afterward, run below command to verify that Docker is installed:
1
dcoker -v

Setting Up a Dockerfile

Now we need to setup Docker file to build our Docker Image for our Node.JS apllication. An image represents an immutable snapshot of an environment that contains all the source code, dependencies, and other files needed for an application to run. Once a Docker image is created, it can be transported to another machine and executed there without compatibility issues. Create `Dockerfile` in your application's root and paste below code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
FROM node:16-alpine

# Create app directory
WORKDIR /usr/src/app

# Install app dependencies
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 3000

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

Build the Docker Image

Now that the `Dockerfile` is complete, it's time to build the Docker image according to the instructions in the file. This is achieved through the docker build command. You need to pass in the directory where the `Dockerfile` exists and your preferred name for the image.
1
docker build -t dockerize-nodejs .

If all goes well and the build succeeds, you will see the messages below at the end of the command’s output:

1
2
Successfully built <some hash>
Successfully tagged dockerize-nodejs:latest

You can run docker images to view some basic info about the created image:

1
2
3
docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
dockerize-nodejs latest <some hash> 2 minutes ago 137MB

Run the Docker Image in a Container

Use the `docker run` command to run your newly minted Docker image inside of a container. Since the application has been built into the image, it has everything it needs to work. It can be launched directly in an isolated process. Before you can access your running image inside the container, you must expose its port to the outside world through the --publish or -p flag. This lets you bind the port in the container to a port outside the container.
1
docker run --name dockerize-nodejs -p 3000:3000 dockerize-nodejs:latest

The command above starts the dockerize-nodejs image inside of a container and exposes port 3000 inside the container to port 3000 outside the container. You can subsequently access the routes on your server through http://localhost:3000.

Conclusion

If you have any issue regarding this post, mention your issue in comment section or reach me through my E-mail.

Happy Coding