How to run Node.js applications on Docker Engine

Table of content

  1. What is Node.js ?
  2. What is docker ?
  3. Prerequisites
  4. How to Install Node.js on ubuntu machine
  5. Install Node.js Express Web Framework
  6. Create a Node.js Application
  7. Create a Docker file for Nodejs application
  8. Build Docker Image
  9. Run the Nodejs application on a Container
  10. Conclusion

What is Node.js ?

Node.js is an open source JavaScript runtime environment. Now, what is JavaScript ? Basically JavaScript is a language which is used with other languages to create a web page and add some dynamic features such as roll over and graphics.

Node.js runs as a single process without wasting much of memory and CPU and never blocks any threads or process which is why its performance is very efficient. Node.js also allows multiple connections at the same time.

With the Node.js it has become one of the most advantage for JavaScript developer as now they can create any apps utilizing it as both frontend or as a backend.

Building applications that runs in the any browser is a completely different story than than creating a Node.js application although both uses JavaScript language.

What is docker ?

Docker is an open source tool for developing , shipping and running applications. It has ability to run applications in loosely isolated environment using containers. Docker is an application which helps in management of containers in a very smooth and effective way. In containers you can isolate your applications. Docker is quite similar to virtual machine but it is light weighted and can be ported easily.

Containers are light weighted as they are independent of hypervisors load and configuration. They directly connect with machines ie. hosts kernel.


You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install node.js on ubuntu machine

  • Update your system packages.
sudo apt update
  • Lets change the directory and Download the node js package
cd /opt

sudo apt install nodejs
  • Install node js package manager
sudo apt install npm
  • Verify the node js package installation
nodejs -v

Install Nodejs Express Web Framework

  • Install Nodejs Express Web Framework and initialize it
npm init
  • package.json which got created after initializing the Nodejs framework will have all the dependencies which are required to run. Let us add one dependency which is highly recommended.
npm install express --save



var express = require('express')    //Load express module with `require` directive
var app = express() 

//Define request response in root URL (/)
app.get('/', function (req, res) {
  res.send('Hello Welcome to')

app.listen(8081, function () {
  console.log('app listening on port 8081!')
  • Now Run the Node.js application locally on ubuntu machine to verify
node main.js
This image has an empty alt attribute; its file name is image-38.png

Create a docker file for Node.js application

Docker file is used to create a customized docker images on top of basic docker image. It is a text file that contains all the commands to build or assemble a new docker image. Using docker build command we can create new customized docker images . Its basically another layer which sits on top of docker image. Using newly built docker image we can run containers in similar way.

This image has an empty alt attribute; its file name is image-43.png
  • Create a docker file and name it as Docker file . Keep this file also in same directory as main.js
FROM node:7              # Sets the base image
WORKDIR /app             # Sets the working directory in the container
COPY package.json /app   # copy the dependencies file to the working directory
RUN npm install          # Install dependencies
COPY . /app              # Copy the content of the local src directory to the working directory
CMD node main.js         # Command to run on container start  

Build a Docker Image

  • Now we are ready to build our new image . So lets build our image
docker build -t nodejs-image .
  • You should see the docker images by now.
docker images

Run the Nodejs application on a Container

  • Now run our first container using same docker image ( nodejs-image)
docker run -d -p 8081:8081 nodejs-image
  • Verify if container is successfully created.
docker ps -a

Great, You have dockerized Nodejs application on a single container .

This image has an empty alt attribute; its file name is image-38.png


In this tutorial we covered what is docker , what is Nodejs and using Nodejs application created a application on docker engine in one of the containers.

Hope this tutorial will helps you in understanding and setting up Nodejs and Nodejs applications on docker engine in ubuntu machine.

Please share with your friends.