Build a NodeJS Application on Docker

Posted on July 11th, 2020

In this tutorial, we can check how to build a NodeJS Application on Docker

Docker is a set of PaaS (Platform as a Service) products that deliver software packages called containers. Docker’s containerization technology allows you to build, test, and deploy any applications quickly. The applications build and deployed through Docker can run as portable and self-sufficient containers anywhere. Docker has become one of the best tools for container deployment. In the market, there are both free and premium tiers for this service. Docker Engine is the software that hosts the containers. These containers are isolated from one another, and it has its software, libraries, and configuration files. The containers use well-defined channels to communicate with each other.

In this knowledge base, we learn how to build a NodeJS application on your server and later push that created application to the Docker hub. In this knowledge base, we are using Ubuntu 18.04 LTS server as our testing environment.

The Docker helps to ship the created application in containers. This container helps to bundle the applications as libraries and dependencies. The Docker provides a fully-fledged environment to deploy applications, and this environment ensures that the created application works similarly in all the systems like that of the developers.


Ensure you have installed the following on your server before creating and building the application.

  1. NodeJS
  2. Docker
  3. Node Package Manager (npm)

Steps to Build a NodeJS application on Docker

To create an application and then push it on Docker, follow the below steps: 

1) Create a NodeJS Application

Log in to the server as the ssh user with sudo privileges.

First, create a new directory and switch to the newly created directory by executing the following commands.

$ sudo mkdir mynode-app
$ sudo cd mynode-app

Then, run the following npm command to initialize the directory.

$ sudo npm init

The above command creates a ‘package.json’ file and prompts the created JSON file details, such as version, name, description, keywords, and author. After confirmation, type ‘yes’ and confirm the details of the JSON file.

After that, execute the following npm command to add the express framework.

$ sudo npm install express --save

The above command should display an output similar to the one shown in the screenshot below.

Build NodeJS


Create the ‘index.js’ file and then paste the following code inside the file.

const express = require('express')
const app = express()
app.get('/', ( req, res ) => {
app.listen(3000, () => {
    console.log('Listening on port 3000!')

The above file creates a route to the index.html file that listens to the 3000 port.

Now, create the ‘index.html’ file and then paste the following configuration inside the following file.

        <h1>Hello there! Welcome to my NodeJS application</h1>

Now, you have successfully created the NodeJS application.

2) Run the Application

To launch the created NodeJS application, execute the following command.

$ sudo node index.js

The output displays that the application listens to port 3000, and to confirm the same, you can open any web browsers and hit the following URL.



3) Dockerize the NodeJS Application

To Dockerize the created NodeJS application, follow the below steps:

1) Create a Docker file by executing the following touch command.

$ sudo touch dockerfile

2) Then, add the following configuration in the created file.

FROM node:carbon
WORKDIR /usr/src/app
COPY package*.json ./
RUN npm install
COPY . .
CMD ["node", "index.js"]


4) Build the NodeJS application on Docker

After creating the Docker file with the necessary configuration, you need to build the created NodeJS application on Docker using the following command. The ‘-t’ option in the following command helps to tag the application as an image to make it easy to locate when we run the docker image command.

$ sudo docker build -t mynode-app .

Execute the following command to confirm the existence of the added image.

$ sudo docker images


5) Run the Docker Container

Now, the image is ready to launch. To launch the image, run the following docker command.

# sudo docker run -p 8080:3000 -d node-app

The above command makes sure that the application listens to the port 8080 instead of the 3000 port. To confirm, if this change has occurred correctly, browse the following URL in any of the web browsers.


In the above URL, replace the <Server_IP_address> with the actual IP of your Ubuntu server.


6) Push the Created NodeJS Application to the Docker Hub

1) First, ensure that you already have an account at the Docker hub.

2) Next, build the created NodeJS application using the Docker credentials.

$ sudo docker build -t <username>/<tag> .

In the above command, replace the <username> and <tag> flags with the docker hub login name and the tag of the application. Suppose the login name of the docker hub is ‘testuser’, and the tag of the application is ‘mynode-app’, then execute the following command to build the application.

$ sudo docker build -t testuser/mynode-app .

4) Now, log in to the Docker hub by using the following command.

$ sudo docker login

The above command prompts for the username and password, fill the details and then press the ‘Enter’ key.

5) After logging in to the Docker Hub, execute the following command to push the image of the application to the hub.

$ sudo docker push <username>/<tag>

For example,

$ sudo docker push testuser/mynode-app

6) Now, you can see the application’s image in the Docker hub.

We hope that this knowledge base was helpful to you. Please comment below for any questions or queries. If you are an InterServer customer, please reach out to our support team for further help.

Leave a Reply