Dockerization of an Express Application on AWS EC2: A Step-by-Step Guide

Dockerization of an Express Application on AWS EC2: A Step-by-Step Guide

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:

  • Install Docker on an Amazon EC2 instance.
  • Create a Dockerfile for an Express.js application.
  • Build a Docker image from the Dockerfile.
  • Run the Docker image in a Docker container.

Instructions:

Step 1: EC2 Instance Setup

  1. EC2 Instance Creation:

  • Log in to your AWS Management Console.
  • Create a new Amazon EC2 instance (choose a suitable Amazon Machine Image, e.g., Amazon Linux or Ubuntu).
  • Configure security groups to allow SSH access (port 22) and HTTP access (port 80).
  • Launch the instance and download the private key (.pem) for SSH access.

EC2 Instance - Backend App


2. Create RDS :

RDS with PostgresSQL Engine

  • Copy endpoint:

?tutrial-db.cwxox60jm7kg.ap-south-1.rds.amazonaws.com

  • Add inbound rule for RDS- Security group

Add rule with Type as PostgresSQL


  1. Connecting to EC2 Instance:

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:

  1. Docker Installation:

Update the package list on your EC2 instance.

sudo apt update

  1. Install Required Dependencies:

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

  1. Add the Docker Repository GPG Key:

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

  1. Add the Docker APT Repository:

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

  1. Start and Enable Docker Service:

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

https://gitlab.com/angular_application/my-tutorial-be.git

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.

Save file and Hit Enter


Dockerfile is created


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:

  • ?Use the docker run command to run a Docker container from the image you built.
  • Ensure that the container is accessible on port 80 (or a port of your choice).

docker run -d -p 3000:8080 v1

Verify docker logs:

hostname is not found

Copy end point from RDS and update in db.config.js

tutrial-db.cwxox60jm7kg.ap-south-1.rds.amazonaws.com

HOST updated

Create another image v2?and verify docker images?:

Run docker using command : docker run -d -p 3000:8000 v2

Server is up and running

?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.

Deployment is Successful , Application is Up and Running

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.

Sanjeev Mishra

SDET Lead | Java | Selenium | Rest Assured | WebdriverIO | TestNG | Maven | Cucumber | Jenkins | Data Structures

1 年

Awesome

Srividhya C

Test Engineer at Firstsource

1 年

Great work ??

Jatin Shharma

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 ??

malyaz sahu

Engineering Manager at Landmark Group | Warehouse Automation & GDMS | Passionate About Supply Chain Solutions

1 年

It was really insightful, thanks for sharing this.

要查看或添加评论,请登录

Bhagyashree Sahu的更多文章

社区洞察

其他会员也浏览了