Create docker image with nodejs application

Docker is a container which stores images inside it. We create our application image then push it on docker hub.

Docker is a computer program that performs operating-system-level virtualization, also known as “containerization”. It was first released in 2013 and is developed by Docker, Inc.

Ref :

Here in this post we are trying to create image of our node application.

First register in

create a application directory myApplication. Now run below command :

1)npm init //This command will create the package.json

2)npm install express –save //This will install express (nodejs framework)

3)Create app.js inside the myApplication  folder

var express = require('express');
var app = express();
var router = express.Router();
message :"Hello world"
console.log('The server starts ');

4)Now again, inside the myApplication folder , create a file Dockerfile

Note : The Dockerfile file will tell what will contain in our application container.The file starts with FROM . It defines our container environment

FROM node:10-alpine

RUN mkdir -p /home/node/app/node_modules && chown -R node:node /home/node/app

WORKDIR /home/node/app

COPY package*.json ./

RUN npm install

COPY . .

COPY --chown=node:node . .

USER node


CMD [ "node", "app.js" ]

5)Also create .dockerignore. It is similar to gitignore


6)Now , run below command inside the myApplication folder , to create application image

docker build -t 9955158678/mysimpleapplication .      (-t to tag the application image name)

or :

docker image build -t my-angular-app

Note : here 9955158678 is my username in

Note : Here .(dot) means image to create in current location

The above command will create the docker image

7)List all docker images :

docker image ls

8)Now create container from the image

docker run –name mysimpleapplication -p 8083:8080 -d 9955158678/mysimpleapplication

Note : Here 8083 is docker container port number. 8080 is the port of our application

9)After successfully creating the container from image  , list it by below command

docker container ls

10)Now push the container to docker hub

docker login // It will ask for username and password

docker push 9955158678/mysimpleapplication

— done

Note : We have successfully pushed the docker container to the docker cloud hub

11)Once done .. Let us stop the container. So, that we can freshly pull our new application image and run it

docker container stop <container_ref>

12)Now, delete all images related with the container

docker system prune -a

13)As, all the images gets deleted along with the docker container get stopped. Now lets pull fresh docker image from the docker hub

docker pull 9955158678/mysimpleapplication

14)Now after downloading the image , we cal list all the images by below command

docker image ls

15)Now, create the container from the image

docker run –name mysimpleapplication -p 8083:8080 -d 9955158678/mysimpleapplication

Note : SO, our container gets started on port 8083

16)Run the browser by following url : localhost:8083/api/test

By Pankaj Kumar Agarwal

Leave a reply:

Site Footer