Dockerization of an Express Application on AWS EC2: A Step-by-Step Guide
Bhagyashree Sahu
SDET| Immediate Joiner| ISTQB Certified | Quality Assurance | Manual Testing | Selenium | Java | Software, API, Mobile App testing | Rest Assured | Telecom | Insurance | Core Banking and Finance |
Introduction:
Containerization has revolutionized the way we deploy and manage applications, making it easier to ensure consistency and scalability across various environments. Docker, one of the most popular containerization platforms, allows you to package applications and their dependencies into portable containers. In this article, we will explore how to dockerize an Express.js application and deploy it on an AWS EC2 instance for a robust and scalable solution.
Objectives:
Instructions:
Step 1: EC2 Instance Setup
2. Create RDS :
Use SSH to connect to your EC2 instance using the private key you downloaded.
Example: ssh -i your-key.pem ec2-user@your-ec2-instance-ip
Step 2: Docker Installation:
Update the package list on your EC2 instance.
sudo apt update
Docker requires some packages to be installed. You can install them using the following command:
sudo apt install -y apt-transport-https ca-certificates curl software-properties-common
To ensure the authenticity of Docker packages, you should add Docker's GPG key to your system. Run the following command to add the key:
?curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /usr/share/keyrings/docker-archive-keyring.gpg
You can add the official Docker APT repository to your system with this command:
?echo "deb [arch=amd64 signed-by=/usr/share/keyrings/docker-archive-keyring.gpg] https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
?5. Update the Package Database (Again):
After adding the Docker repository, update the package database again to ensure that you can install Docker from the newly added repository:
?sudo apt update
Install Docker using package manager (e.g., yum for Amazon Linux or apt for Ubuntu). Install Docker using the following command:
?sudo apt install -y docker-ce docker-ce-cli containerd.io
Once Docker is installed, start the Docker service and enable it to start automatically on boot:
sudo systemctl start docker
领英推荐
sudo systemctl enable docker
Verify Docker installation with docker --version and docker run hello-world.
sudo docker --version
Step 3: Dockerizing the Express Application
Express Application Setup:
Clone or upload your Express.js application code to the EC2 instance.
Click on clone and Copy link of clone with HTTPS
Create a Dockerfile:
A Dockerfile typically consists of a series of commands and instructions that are used to create a Docker image.
Create a Dockerfile for your Express application on the EC2 instance with command: nano Dockerfile
The Dockerfile should define the base image, set the working directory, copy the application files, install dependencies (if any), and specify the command to start your application.
Build Docker Image:
Use the docker build .? -t v1 command to build a Docker image from the Dockerfile
Step 4: Running the Docker Container
Run Docker Container:
docker run -d -p 3000:8080 v1
Verify docker logs:
Copy end point from RDS and update in db.config.js
Create another image v2?and verify docker images?:
Run docker using command : docker run -d -p 3000:8000 v2
?Add rule for port no 3000 on EC2 Instance:
Testing:
Test the accessibility of your Express application by accessing the EC2 instance's public IP address or DNS name in a web browser.
Conclusion:
Dockerizing and deploying an Express application on AWS EC2 simplifies the process of managing your application's dependencies and ensures consistent deployment across different environments. By following the steps outlined in this guide, you can achieve a scalable and reliable deployment solution for your Express.js applications on AWS EC2.
SDET Lead | Java | Selenium | Rest Assured | WebdriverIO | TestNG | Maven | Cucumber | Jenkins | Data Structures
1 年Awesome
Test Engineer at Firstsource
1 年Great work ??
Helping You Master SDET & DevOps | 10+ Years in Automation Testing | Interview Hacks & Career Growth Tips | Follow for 1% Daily Improvement
1 年Keep up the good work! See you soon for creating the Ci Cd pipeline with docker ??
Engineering Manager at Landmark Group | Warehouse Automation & GDMS | Passionate About Supply Chain Solutions
1 年It was really insightful, thanks for sharing this.