Skip to content

How to Dockerize your node.js application?

In this tutorial you will learn how to containerize/dockerize your existing node js application with Docker. You will also learn how to add instructions to a Dockerfile to dockerize a node.js application

Docker lets you package an application with its environment and all of its dependencies into a container. A container is a basic version of a Linux operating system which would have all the dependencies needed to run a Docker image. An image is the blueprint for a container, a container is a running instance of an image.

To containerize your node.js application, start with creating an empty file with name Dockerfile in the root of your project.

Let’s start adding instructions to this Dockerfile one by one.

1. Specify Node image version

FROM node:14

2. Specify work directory in which your application code will reside and image will be created

# Create app directory
WORKDIR /usr/src/myNodeApp

3. Copy package.json and Install dependencies

# Install app dependencies
COPY package*.json ./

RUN npm install

4. Bundle your app source using COPY command

# Bundle source code
COPY . .

5. Expose your application to a port

EXPOSE 3300

This is the port your application will map to the docker daemon.

6. Specify the commands to run your application

CMD npm start dev

This is where you specify the command to start your node js application. I use npm start to start my application. You can simply use node index.js.

Your Dockerfile should look like this now.

FROM node:13

# Create app directory
WORKDIR /usr/src/myNodeApp

# Install app dependencies
COPY package*.json ./

RUN npm install

# Bundle app source
COPY . .

EXPOSE 3300
CMD npm start

Building Docker image

Run the following command from the directory where your Dockerfile is located to build an image.

docker build -t my_node_app_image .

This will create a docker image with the name my_node_app_image.

Use the following command to see if your image has been created.

docker images
REPOSITORY	                          TAG                 IMAGE ID            CREATED             SIZE
my_node_app_image                    latest              2938e0bbe9a7        15 seconds ago       24MB

Building Docker container from your image

To run your image as a container use the command below.

docker run -p 8020:3300 -d my_node_app_image

-d is to keep your container running in the background. -p is to expose and publish the port. Your application will be exposed at 3300 at the container and 8020 in the host.

To see your container has been created and running, use docker ps command

docker ps


CONTAINER ID        IMAGE                  COMMAND                  CREATED             STATUS              PORTS               		NAMES
d2b3c3c6e6c1        2938e0bbe9a7           "npm start"   About an hour ago   Up About an hour           3300/tcp, 0.0.0.0:8020->3300/tcp               

Now you can access your application from the port 8020.

Fin.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.