How to deploy a Node.js application using a Docker?

Today, if you look around a lot of people, are using the website and web hosting services for their website and the reason behind it is that today a lot of people are using the internet service. So today if you have a website then you can easily showcase your business products and services to the people around you and also to the people across the globe. And today there are a lot of hosting platforms are available where you can choose the right hosting plan as per your requirements. And today, if you are looking for dynamic hosting with the latest technology, means you need to go with Cloud hosting. Yes, the cloud is the latest form of hosting where it works using a network of remote servers hosted on the internet to store, manage and process data.

Using cloud server hosting for your website gives you a lot more advantages in terms of security, reliability, scalability, and flexibility. Not only that using a cloud server for your business will also reduce your operational cost. Moreover using cloud servers you can easily access your data at any time at any place with the help of internet.

Today we will discuss on how to deploy a Node.js application using a Docker in the cloud server. Before that let us discuss what is Node.js and docker.

Node.js is a free, open-source server environment and uses javascript on the server and runs on various platform like Linux, Windows, Unix etc. And its Cross-stage runtime condition is based on Chrome’sV8 javascript motor. Using you can create a quick, versatile server-side web application and it is also a highly scalable server-side application using javascript. It is lightweight and effective as it is the occasion -driven and non-blocking I/O display.

A Docker allows the package an application with all of its dependencies into a standardized unit, called a container, and this container is a stripped-to-basics version of a Linux operating system. The software which is loaded into the container is called an image. Here a Docker image will be built for the application and the image runned as a container.


Follow the below steps to deploy a Node.js application using a Docker in cloud server:

Step 1: Create your Node application:

  Step 1.1: Log-in to your SSH and

  Step 1.2: Create a directory called src

                    $  mkdir src

   Step 1.3: Change directory (this will be the root directory for the project)

                     $ cd src


Step 1.4: Create a file name package.json and add the following using the editor  (You can edit and add, your name, description, author)

               $ nano package.json

Add the following:


"name": "hey_there",

"private": true,

"version": "0.0.1",

"description": "hey there Example",

"author": "",

"dependencies": {

      "express": "3.2.4"



Step 2: Create an index.js  and change the port to 8080 ( or to any other port which is required)

        Step 2.1: Create index.js using the below:

                                        $ nano index.js

        Step 2.2: Add the below


var express = require('express');

var app = express();

app.get('/', function (req, res) {

  res.send('Hello from Docker\n');



console.log('Running on http://localhost:8080');

Step 3: Create a docker file where it allows you to build and deploy a docker image based on the set of instructions you have provided within it.

Step 3.1: create a file named Dockerfile  and populate it with the below command

                                 $ nano Dockerfile

Step 3.2: Add the below code

FROM centos:latest

MAINTAINER Name Here <[email protected]>

RUN rpm -Uvh

RUN yum install nodejs npm -y

COPY ./src /opt/src

RUN cd /opt/src; npm install


CMD ["node", "/opt/src/index.js"]

The explanation for the above code:

FROM centos:latest :  will use an image called centos. In case there is no image/pic available on your host, Docker will pull one from its repository.

MAINTAINER Name Here <[email protected]>: This instruction allows you to set the author for the image being created.

RUN rpm -Uvh

RUN yum install nodejs npm -y

RUN cd /opt/src; npm install

These instructions will  "RUN" file will execute the shell commands given. Now you have to download the epel-release rpm and install it and also install nodejs and npm from its repository.

COPY ./src /opt/src:  To Copy the directory from the host into the container.

EXPOSE 8080: Open port 8080 for the outside world to use.

CMD ["node", "/opt/src/index.js"] :  Once you open the port CMD will provide default execution for the container.

Step 4: To Build image run this command

docker build -t name/application

Step 5: To deploy and test the application by using the  following command

docker run -p 89110:8080 -d name/application

Step 6: To test the application so to run that applicant follow the below command


If you see Hello from Docker then you have successfully deployed your Docker image.


Following the above steps, you can deploy a Node.js application using a Docker.