Dockerizing a Node.js web application

In this article, we will see how to dockerize a Node.js application. Dockerizing a Node.js web application

Further, to Learn more about Node.js

Implementing Redis Pub/Sub in Node.js

Apache Kafka for Node.js Developers

what is docker ?

Firstly, Docker is containerization platform where developers can package the application and run as a container.

In simple words, docker runs each application as a separate environment which shares only the resources such as os, memory, etc.

docker node

Virtual Machine vs Docker

Docker and node.js setup

Here, we can find the difference between the docker and virtual machines.

To read more about docker, Docker Docs

we are gonna see how to dockerize a node.js application. before that, docker has to be installed on the machine. Docker Installation

After installing the docker, we need to initialize the node application.

1npm init --yes
2npm install express body-parser

the first command initializes the package.json file which contains the details about the application and dependencies. the second one install the express and bodyParser

create a file called server.js and paste the following code

1"use strict"
2
3const express = require("express")
4
5// Constants
6const PORT = 8080
7const HOST = "0.0.0.0"
8
9// App
10const app = express()
11app.get("/", (req, res) => {
12 res.send("You have done it !!!!!\n")
13})
14
15app.listen(PORT, () => {
16 console.log(`Running on http://${HOST}:${PORT}`)
17})

this runs the basic express application server. now, we need to create the docker image file. create a file name called Dockerfile and add the following commands

1FROM node:8

First we install the node image from the Docker hub to the image

1WORKDIR /usr/src/app

Next, we set the /usr/src/app as the working directory in the docker image

1COPY package*.json ./
2RUN npm install

then copies the package.json from the local machine to docker image. It's not an efficient way to copy the dependencies from local to docker image.

so we are just copying the package.json and install all the dependencies in the docker image

1COPY . .
2EXPOSE 8080
3
4CMD [ "npm" , "start" ]

it copies all the source code from local to docker image, binds the app to port 8080 in the docker image. docker image port 8080 can be mapped with local machine port. then we run the command

Your Dockerfile should now look like:

1# this install the node image from docker hub
2FROM node:8
3# this is the current working directory in the docker image
4WORKDIR /usr/src/app
5#copy package.json from local to docker image
6COPY package*.json ./
7#run npm install commands
8RUN npm install
9#copy all the files from local directory to docker image
10COPY . .
11#this port exposed to the docker to map.
12EXPOSE 8080
13
14CMD [ "npm" , "start" ]

create a .dockerignore file with the following content:

1node_modules
2npm-debug.log

now, we need to build our image in the command line as :

1$ docker build -t <your username>/node-web-app .

-t flag used to tag a name to image. so, it will be easy to identify with a name instead of id. Note : dot in the end of command is important(else it won't work)

we could run the image using the following command :

1docker run -p 49160:8080 -d <your username>/node-web-app

we can check it using

1curl -i localhost:49160

output should be:

1HTTP/1.1 200 OK
2X-Powered-By: Express
3Content-Type: text/html; charset=utf-8
4Content-Length: 23
5ETag: W/"17-C2jfoqVpuUrcmNFogd/3pZ5xds8"
6Date: Mon, 08 Apr 2019 17:29:12 GMT
7Connection: keep-alive
8
9You have done it !!!!!

To read more

https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md

To Read More

Everything you need to know about d...

Docker volume is a persistent data storage mechanism to store the data in docker...

How to remove docker image - Docker...

This guide will cover everything you need to know about removing docker images f...

How to build an Actionable data ta...

In this article, we will see how to build an Actionable data table using a react...