Make a Simple docker image and put it into a container

Hello folks,In this post i’m going to write about making a simple Node JS docker image and dockerizing it.Lets make a simple  Node application first.

Lets make a folder for our project my case it is “docker-node”, then execute these commands to start node application in it.

this will initiate node application for you. you just need to give answers for simple questions which it shows in console. (To run these commands you need to have installed node in your machine (globally) , node comes with it’s package manager call npm)

npm init

It makes package JSON file.
install express framework

npm install --save express

Now my package JSON file looks like this.

  "name": "docker-node",
  "version": "1.0.0",
  "description": "Node.js on Docker",
  "author": "First Last <>",
  "main": "server.js",
  "scripts": {
    "start": "node server.js"
  "dependencies": {
    "express": "^4.13.3"

Make a simple web app using Express JS framework

'use strict';

const express = require('express');

// Constants
const PORT = 8080;

// App
const app = express();
app.get('/', function (req, res) {
  res.send('Hello world\n');

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

Now we are going to put this little application inside the docker container.

Create a file and name it as “Dockerfile” (no extensions) Open the “Dockerfile”.

Now we have to define the base layer of our image (most required software piece) . If we are building a java application which java version we need. In this case we need o install node js to run our application so we are going to use node : boron version. Here you can see available node versions in docker hub.

 FROM node:boron

Now we have to define application directory to hold application files inside the image.

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

Node:boron image is coming with Node and npm  so now we only need to install application dependencies that contains in package.json file. `COPY` command is use to copy package.json file into image’s application folder.

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

To add other source codes into the docker image you have to add this command into Dockerfile.

 # Bundle app source
COPY . /usr/src/app

now we have to bind one  port of container to expose our do that we have to use this.


We have to run the our Node JS application after we run Docker image for that we need to execute ‘npm start’ command , include that command in Dockerfile like this.

CMD [ "npm", "start" ]

Finally our Dockerfile is like this.

FROM node:boron

# Create app directory
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app

# Install app dependencies
COPY package.json /usr/src/app/
RUN npm install

# Bundle app source
COPY . /usr/src/app

CMD [ "npm", "start" ]

in our application there are few folders and files that should not be included in our docker image. we can include those files and folders in .dockerignore file just like this


Build Our image.

To build docker image run this command.

docker build -t reponame:docker-node .

In this command `-t ` is for option that we use to add a tag to image.we use tag for uniquely identify the image. (don’t forget to type full stop which is in end of the command . this full stop says that our Dockerfile is here)

run this on your terminal to check whether docker image is built or not.

docker images

it will show docker images you have like this.

user@isuruab:~$ docker images
REPOSITORY                                            TAG                 IMAGE ID            CREATED             SIZE
reponame                                              docker-node         fbcbf342c428        8 days ago          662.5 MB

Run docker image.

docker run -p 49000:8080 -d reponame:docker-node

in this command -p for bind public port to private port (49000 is our local machine’s port, 8080 is the port that expose our application in docker container ) -d option is for run docker image in detached mode (it runs in background you can use terminal for do other operations, if not it shows operations we do in container). you can simply check our image by moving to http://localhost:49000/
It will show “Hello world” on your browser.

you can check docker containers and relevant IDs by executing this command.

docker ps

output :

user@isuruab:~/Desktop/node$ docker ps
CONTAINER ID        IMAGE                  COMMAND                  CREATED              STATUS                             PORTS                     NAMES
2b38098704af        reponame:docker-node   "npm start"              About a minute ago   Up About a minute        >8080/tcp   naughty_brown

we can check what container logs by this command:(2b38098704af is my container ID which i got it from previous command)

docker logs 2b38098704af


user@isuruab:~/Desktop/node$ docker logs 2b38098704af
npm info it worked if it ends with ok
npm info using npm@3.10.10
npm info using node@v6.10.0
npm info lifecycle docker-node@1.0.0~prestart: docker-node@1.0.0
npm info lifecycle docker-node@1.0.0~start: docker-node@1.0.0

> docker-node@1.0.0 start /usr/src/app
> node app.js

Running on http://localhost:8080

To go inside the container

user@isuruab:~/Desktop/node$ docker exec -it 2b38098704af /bin/bash

output will be like this


to come back to local machine use


That is all for today. Next time I will post an article about docker swam. tata for now 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s