Project 2: Deploying a node application Using Docker
Step 1: Launch an EC2 Instance
With the help of this: https://lalitakashyapblog.hashnode.dev/step-by-step-guide-to-setting-up-an-ec2-instance
Step 2: Install docker
With the help of this: https://lalitakashyapblog.hashnode.dev/from-zero-to-docker-a-beginners-introduction-to-containers
Step 3: Create directory and move into it
mkdir projects
cd projects/
Step 4: Create project name directory and move into it
mkdir node-app
cd node-app/
Step 5: Clone repository url
git clone https://github.com/Lalita5Kashyap/node-todo-cicd.git
Step 6: Move into project folder
cd node-todo-cicd/
Delete Dockerfile if have.
Step 7: Create dockerfile
#Get a base image with nodejs
FROM node:14
#Create a working directory to run a node app
WORKDIR /app
#Copy code from local machine to docker container
COPY . .
#Install npm
RUN npm install
#All my test cxases are passing
RUN npm run test
#Expose port
EXPOSE 8000
#Run the node app
CMD ["node", "app.js"]
Step 8: Build image
docker build -t node-app:latest .
Step 9: Check image created or not
docker images
Step 10: Create and run docker container
docker run -d node-app:latest
Step 11: Check container created or not
docker ps
Step 12: Map container port to system port
docker run -d -p 8000:8000 node-app:latest
Step 13: Give permission to port "8000" in instance
Go to security group in instance
Click SG link
Click on "edit inbound rules"
Click on "Add rules"
Give "8000" in port range and select "Anywhere" in source and click Save
Copy public dns from instance in networking
Now go to browser and paste public ip of instance
We hope this guide has provided you with a solid foundation to start deploying Node applications with Docker. Happy coding!
Thank you ๐
Keep Learning..
Subscribe to my newsletter
Read articles from Lalita Kashyap directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by