Venketraman
venket's b log

venket's b log

Dockerizing The Node.JS Application

Dockerizing The Node.JS Application

Venketraman
ยทJul 31, 2021ยท

3 min read

Play this article

We all are familiar with Node.JS Application. still most of us using to deploy our node applications in virtual servers or physical servers.

Here i am going to explain how to create sample Node.js application and tested it. After that how to Dockerizing the normal node js Application.

First install Node, npm packages and Docker Service on the same server .

If you not aware how to install that packages check my previous posts. ๐Ÿ‘‡

Node installation

Docker Installation

Create the Sample Node Application

  • First we create the sample node.js Application for that follow the below steps.
  • Create one directory inside that initialize the NPM. Its asks some question just give enter for all that.
mkdir app
cd app
npm init

Output:

image.png

  • Then we are going to execute the below command to create its dependencies.
npm install express --save

Output:

image.png

  • Now we want to create one sample .js file. i have create shivam.js with the below content.
vi shivam.js
var express = require('express')
var app = express()


app.get('/', function (req, res) {
  res.send('Shivam World!')
})

app.listen(8080, function () {
  console.log('app listening on port 8080!')
})
  • Now i am going to run the application.
node shivam.js
  • Now its running

Output: image.png

  • Check it in terminal using curl command.
 curl http://localhost:8080

image.png

  • Now check it using public URL.

image.png

So our node application is running in server environment. We are going to Dockerizing our node application.

Dockerizing Node App

  • For that first we want to create Dockerfile
  • You just add the below content in to that Dockerfile.
FROM node:12
WORKDIR /app
COPY package.json /app
RUN npm install
COPY . /app
CMD node shivam.js
EXPOSE 8080

Here I am going to explain what is inside the Docker file.

  1. FROM node:12 = we will pull the Node 12 docker image from docker hub

  2. WORKDIR /app = set Work directory as /app directory in side the container.

  3. COPY package.json /app = copy the package.json file from our ubuntu os current directory to inside the container.

  4. RUN npm install = Run the command npm install

  5. COPY . /app = copy all the files from our host ubuntu pc current directory to container workdir

  6. CMD node shivam.js = execute the command node shivam.js to run our node application

  7. EXPOSE 8080 = Expose the port 8080 in that container to host the app via web.

  • Now build the image with Dockerfile

Syntax: docker build -t [image name] shivam [docker file location]

docker build -t shivam .

Output:

image.png

  • Now run the container with port number

Syntax: docker run -p [portno] [public port]80:8080[container port] [image name] shivam

docker run -p 80:8080 shivam

Output:

image.png

  • Now we are going to check in public ip.

image.png

Yeah its working on 80 port.

Hurrah..... we are successfully Dockerizing the node application.

Did you find this article valuable?

Support Venketraman by becoming a sponsor. Any amount is appreciated!

Learn more about Hashnode Sponsors
ย 
Share this