How to Dockerize a Node.js Web App 785X391

Creating a powerful application is possible once you choose to explore the options that exist and Docker with Node.js make an excellent combination. Your node.js applications can improve once you create Docker images which are inserted into the app through a Docker file. Want to know how this works? Follow this step by step tutorial.

Get Started on Node.js

To get started, you will need to create a new Node.js application. This requires you to start off with a directory, and follow with a package.json file. The application that you create should not be complex in nature. Choose a Hello-World app, and ensure that it only has one service dependency. The best place to carry this out is on a development machine such as GitHub. Your initial code shall be as follows: –

$ git clone[your_user_name]/docker-tutorial.get
$ cd docker-tutorial
$ ls
app.js package.json

Creating the Docker File

Start by creating an empty file and name it Docker file. This file shall be positioned at the bottom of your project directory. You will use the following code

FROM nodesource/node:4.0

This line of code is where your Docker image shall be built. The from statement is important as without it, you will not be able to begin your Docker file. The number 4.0 specifies the version of node that you are using, so you can amend this to suit what you have available.

ADD package.json package.json

This makes up the next line of code and is responsible for taking the files and folders out of your existing working directory and placing them into the Docker image in your chosen location. The repetition of package.json is simple an indication of what the Docker image will be called once it has been moved.

RUN npm install

This is the command that ensures your dependencies are installed so that you can commit certain alterations to the image.


Once the installations are complete, then this fourth command ensures that any other source files are added to the Image.

CMD [“node”,”app.js”]

This is the last part of code you will input, and this is the command to use in order to start the application. The application will begin using CMD which is what tells Docker the way you want your application run.

The final code will therefore look like this: –

FROM nodesource/node:4.0

ADD package.json package.json
RUN npm install
ADD. .
CMD [“node”,”app.js”]

Also Check: 10 Best Web Hosting Platforms for Node.js Apps

Building your Docker Image

Once you completed the creation of your Docker image and defined its parameters, the next step to take is to actually build your image.

The first step to building your image is running a specific code in the directory which holds your Docker file. For this example, you will begin with the following: –

$ docker build -t /“myimage”.

From this code you will have taken the right step and created your Docker Image. The -t flag will help you tag your image so that when you need to search for it using the docker images command, it is easy to find. “myimage” is the name that you have given your Docker Image so that you know exactly what to search for. The . at the end is also significant as it reveals that the Docker file that you are using has been located within your current working directory.

The way that you image will be listed within Docker is shown below:

$ docker images

# Example
REPOSITORY                          TAG        ID              CREATED
myimage                            myimage6    539c0211cd76    7 weeks ago
/”my image”		   latest     d64d3505b0d2    3 hours ago

This information is adequate for an environment that can power your app in node.js. With the image built, the next step is to run the image.

How to Run the Image

This is the code you will use to run the image that you have created:

$ docker run -p 49160:8080 -d /”my image”

Using -d to run the image ensures that your image runs with its container in detached mode, which leaves the container to run in the background. Use of -p flag results in a private port in the container running the flag, since it has been redirected from a public port.

Sharing your Image

You have several options to explore once you have run your Docker Image. You can choose to share it in the Docker registry, so that other people on the globe can access and use it.

To do this, log on to Follow the instructions given and create your account. You will need to rebuild the docker image again and give it a new name so that it can be accessed globally. To ensure that your image ends up on the registry, you will need to input the following code in your hub docker account: –

$ docker login
Username: your_user_name
Email: your_email@doo.far
Login Successful!
$ docker push “your_user_name/myimage”

You may also choose to print out your application so that you can have a record of the output. If you choose to print it out, you can use the following code: –

$ docker ps
$ docker logs 

Running on http://localhost:8080

Testing the Dockerized application

You can now test your application to see how the Docker image has turned out. To do this, you will need to identify the port which Docker has mapped. This information already exists in your code, where 8080 is the number that the port of the container 49160 was mapped to.

Dockerizing a Node.js application is simple and can be done by beginners who are just starting out with node.js web applications. This is because it does not require extensive codes, so in four easy steps, you are able to dockerize a node.js web application with excellent images. Furthermore, it is flexible and opens up the application to customization that will make it stand out from all other apps. Even though it takes a minimal time to get started, the final results can be astounding.