Deploy NodeJS APP Using Docker in EC2
“Developer Friendly Deployment Doc’s”
This article is focused on the deployment of NodeJs Application in AWS EC2 using Containerization by Docker in development mode.
Prerequisites to Proceed Further
- Basic in NodeJs
- Basic in Linux Commands [Am describing based on Linux Machine]
- Basic in Dockers and Its Build,Push command
- Docker Hub Account
- Basic in AWS EC2
Create and Dockerize Your NodeJs Application
You need a Basic NodeJs App to do this for that, create a nodejs app following the below
create a folder here am creating a folder of name node-ec2, inside that open terminal and run the bellow command,
npm init
after running the npm init you can saw a file named package,json edit the file and add the below line code inside the scripts section as the below image shows
"start": "node index.js",
Then run the below command,
npm install express --save
which install the npm modules, Then next you have to create a index.js file and add the below sample code
const express = require('express');const app = express();const port = 3000;app.get("/", (req, res) => {res.send("<h1> NodeJS Demo Docker Apllication Successfully Deployed</h1>");});app.listen(port, () => console.log(`Example app listening on port ${port}!`));
You can run npm start, if you want to check the code is working perfectly or not
Next we want to Containerize the application using docker for that you need to install the docker in your system by referring the below link
Click To Install Docker
After successful installation you need to create a Dockerfile in your root of the source code as shown below image
FROM node:16-alpineWORKDIR /appCOPY package.json .RUN npm installRUN npm install pm2 -gCOPY . ./ENV PORT 4000EXPOSE $PORTCMD ["pm2-runtime", "start", "index.js", "--name", "node-ec2"]
and save the code which Named as “Dockerfile” and run the below command to build the docker image
docker build -t akshaykrisondevops/node-ec2 .
where am giving a tag name to the image of akshaykrisondevops/node-ec2 since my dockerhub username is akshaykrisondevops and node-ec2 indicate the project name, Then wait until the build complete and give a success message like the below image,
Then you can check the build-ed image by running the below docker command
docker images
In that you can saw the two images one is for node 16-alpine official image and another one is for our NodeJs Application image which is named as akshaykrisondevops/node-ec2 [name is given by tag]
Then you have to check the build-ed image is working perfectly or not by simply run the image using the docker run command
docker run -p 3000:3000 -d akshaykrisondevops/node-ec2
which run the docker and show an out put like this
Next we have to check the running Containers for that run the command below to list the active conatienrs
docker ps
If you get like the above images you All set, You have successfully build and run your project using the docker container now we can check the docker container is running by typing localhost:3000 in your browser, since my project running on port 3000 i have to run 3000.
Create an Account in DockerHub
You can simply create a dockerHub account using the DokcerHub Signup
After the Signup login to the Account and click on the Repositories and Click on the Create Repository to create a new docker repository
Then you will get a screen like above in that the Point 1 you have to add a name to your repository here i given my project name as node-ec2 and the 2nd Point make your project in private mode that is only you can view or update the project and click on the last one to create a repository
After all step you will get a page like above Next you have to push the build-ed docker image to DockerHub for that you have to login the docker CLI using your terminal
docker login
run the above command in your terminal and input the username and password for login the docker-hub CLI as the below image shows,
Next run the below command to push the docker image in to DockerHub
docker push akshaykrisondevops/node-ec2:latest
Wait until the docker image is pushed, after successful of image push you can check the image is updated in the dockerhub and you can saw the pushed image in there
All set from your local Machine and Next we have to Start the Deployment in AWS EC2
Create an EC2 Instance and Deploy the Docker APP from DockerHub
Login the AWS console and Navigate to the EC2 Instance and Click on the Launch Instance
Choose the AWS Marketplace as shown below image
Search ecs in the search box and select the Amazon ECS-Optimised image as shown in the below image
Next you have to add the configuration details for your instance since am selecting the default configurations and click on the Add Storage option as like the below image shown
Next you have to add the storage you want here am go with 10GB of SSD storgae and Click on the Next button
Next we have to configure the security groups add Custom TCP and set the port rang as Your Application Port range, Since my application runs on Port 4000 i given the Port range as 4000 and Source is selected as Anywhere then Click on the Review and Launch also keep the pem file you have downloaded during the instance creation.
Wait Until the Instance Creation complete…
after the completion click on the Instance ID and you will Navigate to the Page below shows the 1. Indicate the IP of your server, 2. Indicate the connection details of the instance, Click on the Connect button
Then you will get a popup modal like the below image shown Copy the command 3 and Open a terminal where your pem is located and paste it
chmod 4000 docker-nodejs.pem
copy the command marked in red box in the above image and paste it in your terminal to open the SSH connection to the Server
When you successfully enter in to the server check the images are any available in that for that run the docker command [ You don’t have to Install Docker on this Instance Since the ECS comes inbuilt docker and its services ].
Next we have to Login the docker CLI with our Docker hub account. for that run the below command
docker login
Enter the Credentials to Login the docker CLI as like the below image shows
Next we have to pull the docker images by docker pull command and our image name in docker hub with the tag and click enter to pull the image from docker hub to AWS ECS
docker pull akshaykrisonzdevops/node-ec2:latest
Again check the images are available in the server by running, if the image name is showing as like the below image your image pull is successfully completed
docker images
Next we have to RUN this server for that run the below command inside the server
docker run -p 4000:4000 -d akshaykrisondevops/node-ec2
Once the run completed check the docker ps command to check the running is successful
docker ps
The Run you IP with Port in borswer to Load the website…