Share on Social Media

In this guide, you will learn how to install nodejs in docker and run web apps in containers. #centlinux #linux #docker

What is Nodejs?

Node.js is an open-source JavaScript runtime environment. Node.js is used to develop server side scripts and it is used to create dynamic web applications. Node.js represents a “JavaScript Anywhere” paradigm, unifying web application development around a single programming language. (courtesy: Wikipedia)

By following the trend, Nodejs also provides the Official Docker images for creating Nodejs containers.

If you are new to Docker platform, then you should read Docker in Action (PAID LINK) by Manning Publications.

Environment Specification:

We are using a preconfigured Docker Host with following specification.

  • CPU – 3.4 Ghz (2 cores)
  • Memory – 4 GB
  • Storage – 20 GB
  • Operating System – Ubuntu Server 18.04 LTS
  • Hostname –
  • IP Address – /24

If you want to see how to configure a Docker Host, the you can follow our following articles.

Create a Directory for Nodejs Project:

Connect with Docker Host ( as an admin user by using a ssh tool.

Create a directory for our Node.js Docker Project.

ahmer@docker-01:~$ mkdir ~/nodejs_docker
ahmer@docker-01:~$ cd nodejs_docker

Develop Node Web App:

To keep things simple, we are writing a “HelloWorld” node web application for our Node.js Docker container.

ahmer@docker-01:~/nodejs_docker$ nano myapp.js

And add following lines of code to create a simple “Hello World” application.

'use strict';

const express = require('express');

// Constants
const PORT = 3000;
const HOST = '';

// App
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!');

app.listen(PORT, HOST);
console.log(`Running at http://${HOST}:${PORT}`);

Create the Node.js package.json file. This file provides the meta data information for our Node.js Docker application.

  "name": "myapp",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "Ahmer Mansoor <>",
  "main": "myapp.js",
  "scripts": {
    "start": "node myapp.js"
  "dependencies": {
    "express": "^4.16.1"

Create a Dockerfile for Nodejs Docker Project:

To define the deployment procedure such as docker image to be used, dependencies, service port, etc. We are required to create a Dockerfile for our Nodejs Docker container.

ahmer@docker-01:~/nodejs_docker$ nano Dockerfile

Add following directives therein (each directive is briefly described in the comments before it).

# Docker Image to be used
FROM node:13-alpine

# Create a working directory within NodeJS Docker container
WORKDIR /usr/src/app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied where available (npm@5+)
COPY package*.json ./

RUN npm install

# Bundle the App source
COPY . .

# Expose the port 3000 to network computers

#Run the node command and start our web application
CMD [ "node", "myapp.js" ]

We are using the node:13-alpine image here, because it is the lightest image that will fulfill our project’s requirement.

However, there are many variants of the node images are available at the Docker Hub. To get the complete list, you can visit the Official Docker Images for Nodejs Page.

Build the Nodejs Docker Image:

We have created the Dockerfile and the Node.js web application.

Now build the Docker image for our Node.js web application by using the Dockerfile.

ahmer@docker-01:~/nodejs_docker$ sudo docker build .
Sending build context to Docker daemon  4.096kB
Step 1/7 : FROM node:13-alpine
 ---> 483343d6c5f5
Step 2/7 : WORKDIR /usr/src/app
 ---> Using cache
 ---> bad78ca928c6
Step 3/7 : COPY package*.json ./
 ---> Using cache
 ---> fdd7ac2c1274
Step 4/7 : RUN npm install
 ---> Using cache
 ---> 8a34ec80bd87
Step 5/7 : COPY . .
 ---> 873f9ddd4360
Step 6/7 : EXPOSE 3000
 ---> Running in 7d567e0f4da3
Removing intermediate container 7d567e0f4da3
 ---> b3e087682594
Step 7/7 : CMD [ "node", "myapp.js" ]
 ---> Running in 4e170d3a1567
Removing intermediate container 4e170d3a1567
 ---> 7be386175292
Successfully built 7be386175292

We have successfully built our Node.js Docker image.

We have built this image locally, however you can build and store this image on Docker Hub by using your Docker Hub credentials.

Install Nodejs in Docker Container:

Start a container by using above Docker image.

ahmer@docker-01:~/nodejs_docker$ sudo docker run 
> --name nodejs-docker-app 
> -p 80:3000 
> -d 
> 7be386175292

We have started our Node.js Docker container. Check its log now.

ahmer@docker-01:~/nodejs_docker$ sudo docker logs nodejs-docker-app
Running at

Test the Dockerized Nodejs Application:

Access the Dockerized Nodejs application using the curl command.

ahmer@docker-01:~/nodejs_docker$ curl localhost:80
Hello World!

Our Node.js application has been deployed in a Docker container.

Conclusion – Install Nodejs in Docker:

In this guide, you have learned how to install Nodejs in Docker container. Remember that, this is a beginner example of deploying Nodejs web application. But the same procedure can be followed for deploying the larger web applications in the Docker containers.

Leave a Reply

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