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
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.
Virtual Machine vs Docker
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 --yes2npm 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"23const express = require("express")45// Constants6const PORT = 80807const HOST = "0.0.0.0"89// App10const app = express()11app.get("/", (req, res) => {12 res.send("You have done it !!!!!\n")13})1415app.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 808034CMD [ "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 hub2FROM node:83# this is the current working directory in the docker image4WORKDIR /usr/src/app5#copy package.json from local to docker image6COPY package*.json ./7#run npm install commands8RUN npm install9#copy all the files from local directory to docker image10COPY . .11#this port exposed to the docker to map.12EXPOSE 80801314CMD [ "npm" , "start" ]
create a .dockerignore file with the following content:
1node_modules2npm-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 OK2X-Powered-By: Express3Content-Type: text/html; charset=utf-84Content-Length: 235ETag: W/"17-C2jfoqVpuUrcmNFogd/3pZ5xds8"6Date: Mon, 08 Apr 2019 17:29:12 GMT7Connection: keep-alive89You have done it !!!!!
To read more
https://github.com/nodejs/docker-node/blob/master/docs/BestPractices.md
No spam, ever. Unsubscribe anytime.