top

How to Dockerize A Node Js App

IntroductionDocker is an excellent tool for developers. It has significantly enhanced the way we build, deploy, and run applications. There are n number of developers who have been worked with Docker and understood its benefits. But don’t worry, if you have never got an opportunity to use it. Basic understanding of Javascript and Docker is enough to understand this concept. In this tutorial, I’m going to show you how to dockerize a Node Js application from A to Z.ProjectI’m going to use a simple Node Js application which can be found here.Docker InstallationMacOS:- Download and install the dmg file here.Ubuntu:i. Add the GPG key for the official Docker repository to the systemcurl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -ii. Add the Docker repository to APT sourcessudo add-apt-repository “deb [arch=amd64]https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”iii. Update the package database with the Docker packages from the newly added reposudo apt-get updateiv. Run apt-cache policy docker-ce to make sure that you are installing from the Docker repository.https://github.com/collins-b/blogpostv. Finally, install Docker using the command: sudo apt-get install -y docker-ce. And confirm if Docker is running by running the command sudo systemctl status docker Windows: Download and install using the Windows installer here.Running the Application without Dockeri. Clone this project: git clone https://github.com/collins-b/blogpost.gitii. cd blogpost iii. Run npm install, to install packages, then run npm start. Then visit http://localhost:3000, where you should see something similar to the below image:Dockerizing the ApplicationWe’re going to use the project you cloned above. Now, let’s see how to integrate Docker into your existing project.i. In the root directory, create a file named Dockerfile, if the name does not exist. This is a text document that contains all the commands that a user could call on the command line to assemble an image.ii. Populate the file with the following contents:FROM node ENV NPM_CONFIG_LOGLEVEL warn RUN mkdir -p /usr/src/app EXPOSE 3000 WORKDIR /usr/src/app ADD package.json /usr/src/app/ RUN npm install --production ADD . /usr/src/app/ ENTRYPOINT ["npm", "start"]The commands are explained below:FROMThe base image for building a new image in our case is the node. This command must be on top of the Dockerfile.ENV- It defines an environment variable.RUN- This is used to execute a command during the build process of the docker image.ADD- Copies a file from the host machine to the new docker image. ENTRYPOINT- Defines the default command that will be executed when the container is running.WORKDIR- This is directive for ENTRYPOINT command to be executed.EXPOSE- Exposes a specified port on which the container will run.Next, run docker build -t web-app to build a Docker image. Don’t forget to include the period. You can tag your image with any name. Here, I have tagged web-app as shown in the command:                                                               Docker buildNext, we’re going to create a Docker container by running the created image and by using the command below:docker run -d -p 3000:3000 web-appThis creates a writeable container layer over the specified image and then starts using the specified command.I’m using -d flag to run the container in background and print container ID. -p specifies the port in this case and here I’m exposing port 3000.                                                 Docker run                                                         Visit http://localhost:3000 to access the application on your favorite browser. Yes, now you have successfully dockerized the application!Now is time to check the contents of the created container. This is to prove all the files are in the container.List the running containers using the command docker ps.                                                                 Running containerThen, execute into the container by using the container ID and list the files, as shown below:                                                         Container shellYou should see all the project’s files, as shown in the above image.ConclusionWe have successfully dockerized a NodeJs application. The most important configuration is the Dockerfile where we have passed the various commands that are useful in provisioning a Docker image. If you are new to Docker, I will recommend you to visit their online documentation to get a deeper understanding of various Docker components.
Rated 4.0/5 based on 12 customer reviews
Normal Mode Dark Mode

How to Dockerize A Node Js App

Delan collins
Blog
09th Jul, 2018
How to Dockerize A Node Js App

Introduction

Docker is an excellent tool for developers. It has significantly enhanced the way we build, deploy, and run applications. There are n number of developers who have been worked with Docker and understood its benefits. But don’t worry, if you have never got an opportunity to use it. Basic understanding of Javascript and Docker is enough to understand this concept. In this tutorial, I’m going to show you how to dockerize a Node Js application from A to Z.

Project

I’m going to use a simple Node Js application which can be found here.


Docker Installation

MacOS:- Download and install the dmg file here.

Ubuntu:
iAdd the GPG key for the official Docker repository to the system

curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -

ii. Add the Docker repository to APT sources

sudo add-apt-repository “deb [arch=amd64]https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable”

iii. Update the package database with the Docker packages from the newly added repo

sudo apt-get update

iv. Run apt-cache policy docker-ce to make sure that you are installing from the Docker repository.https://github.com/collins-b/blogpost

v. Finally, install Docker using the command: sudo apt-get install -y docker-ce. And confirm if Docker is running by running the command sudo systemctl status docker 

Windows: Download and install using the Windows installer here.


Running the Application without Docker

i. Clone this project: 

git clone https://github.com/collins-b/blogpost.git
ii. cd blogpost 

iii. Run npm install, to install packages, then run npm start. Then visit http://localhost:3000, where you should see something similar to the below image:

Running the Application without Docker

Dockerizing the Application

We’re going to use the project you cloned above. Now, let’s see how to integrate Docker into your existing project.

i. In the root directory, create a file named Dockerfile, if the name does not exist. This is a text document that contains all the commands that a user could call on the command line to assemble an image.

ii. Populate the file with the following contents:


FROM node
ENV NPM_CONFIG_LOGLEVEL warn
RUN mkdir -p /usr/src/app
EXPOSE 3000
WORKDIR /usr/src/app
ADD package.json /usr/src/app/
RUN npm install --production
ADD . /usr/src/app/
ENTRYPOINT ["npm", "start"]


The commands are explained below:

FROM

The base image for building a new image in our case is the node. This command must be on top of the Dockerfile.

ENV

- It defines an environment variable.

RUN

- This is used to execute a command during the build process of the docker image.

ADD

- Copies a file from the host machine to the new docker image. 

ENTRYPOINT

- Defines the default command that will be executed when the container is running.

WORKDIR

- This is directive for ENTRYPOINT command to be executed.

EXPOSE

- Exposes a specified port on which the container will run.

Next, run docker build -t web-app to build a Docker image. Don’t forget to include the period. You can tag your image with any name. Here, I have tagged web-app as shown in the command:


Docker build

                                                               Docker build

Next, we’re going to create a Docker container by running the created image and by using the command below:

docker run -d -p 3000:3000 web-app

This creates a writeable container layer over the specified image and then starts using the specified command.

I’m using -d flag to run the container in background and print container ID. -p specifies the port in this case and here I’m exposing port 3000.

Docker run                                                   Docker run  

                                                       

Visit http://localhost:3000 to access the application on your favorite browser. Yes, now you have successfully dockerized the application!


Now is time to check the contents of the created container. This is to prove all the files are in the container.

List the running containers using the command docker ps.

Running container

                                                                 Running container


Then, execute into the container by using the container ID and list the files, as shown below:

Container shell

                                                         Container shell

You should see all the project’s files, as shown in the above image.


Conclusion

We have successfully dockerized a NodeJs application. The most important configuration is the Dockerfile where we have passed the various commands that are useful in provisioning a Docker image. If you are new to Docker, I will recommend you to visit their online documentation to get a deeper understanding of various Docker components.



Delan

Delan collins

Blog author
I'm a DevOps Engineer and a Blockchain enthusiast. I like open source technologies. I believe in sharing of knowledge with others.
Website : https://github.com/collins-b

Leave a Reply

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

SUBSCRIBE OUR BLOG

Follow Us On

Share on

other Blogs

20% Discount