How to deploy a Node.js application using a Docker?

Today, if you look around a lot of people, are using the website and web hosting services for their website and the reason behind it is that today a lot of people are using the internet service. So today if you have a website then you can easily showcase your business products and services to the people around you and also to the people across the globe. And today there are a lot of hosting platforms are available where you can choose the right hosting plan as per your requirements. And today, if you are looking for dynamic hosting with the latest technology, means you need to go with Cloud hosting. Yes, the cloud is the latest form of hosting where it works using a network of remote servers hosted on the internet to store, manage and process data.


Using cloud server hosting for your website gives you a lot more advantages in terms of security, reliability, scalability, and flexibility. Not only that using a cloud server for your business will also reduce your operational cost. Moreover using cloud servers you can easily access to your data at any time at any place with the help of internet. And today if you are using a cloud server for your business and if you are looking for an information on how to deploy a Node.js application using a Docker in the cloud server, then here is the complete solution on it.


Follow the below steps to deploy a Node.js application using a Docker in cloud server:


>> First you need to create your Node application, so log in to your SSH and create a directory called src and later create a file name src/package.json by using the below command


{

 "name": "hey_there",

 "private": true,

 "version": "0.0.1",

 "description": "hey there Example",

 "author": "",

 "dependencies": {

       "express": "3.2.4"

  }

}


>> Now you have to create a src/index.js using this command. And in the meantime, you also have to change the port 8080 to anything that you wish.


var express = require('express');

var app = express();

app.get('/', function (req, res) {

   res.send('Hello from Docker\n');

});


app.listen(8080);

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


>> Now you have to create a docker file where it allows you to build and deploy docker image based on the set of instructions you have provided within it. And now create a file named Dockerfile  and populate it with the below command


FROM centos:latest

MAINTAINER Name Here <username@localhost>

RUN rpm -Uvh http://happy.me.in/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

RUN yum install nodejs npm -y

COPY ./src /opt/src

RUN cd /opt/src; npm install

EXPOSE 8080

CMD ["node", "/opt/src/index.js"]


>> Explanation:


FROM centos:latest


>> Here it will use an image called centos. In case there is no image/pic available on your host, Docker will pull one from its repository.


MAINTAINER Name Here <username@localhost>


>> This instruction allows you to set the author for the image being created.


RUN rpm -Uvh http://mirror.pnl.gov/epel/7/x86_64/e/epel-release-7-5.noarch.rpm

RUN yum install nodejs npm -y

RUN cd /opt/src; npm install


>> This "RUN" file will execute the shell commands given. Now you have to download the epel-release rpm and install it and also install nodejs and npm from its repository. Use this command


COPY ./src /opt/src


>>> At present Copy the directory from the host into the container.


EXPOSE 8080


>> Open port 8080 for the outside world to use.


CMD ["node", "/opt/src/index.js"]


>> Once you open the port CMD will provide default execution for the container.


>>> To Build image run this command


docker build -t name/application


>> Now you have to deploy and test the application by using the  following command


docker run -p 89110:8080 -d name/application


>> Now you have to test the application so to run that applicant follow the below command


curl http://132.17.24.26:89110/


>> And now if you see Hello from Docker then you have successfully deployed your Docker image.