Aws RDS with Docker and EC2

Kunal SinghKunal Singh
2 min read

How I Hosted a Node.js Frontend and Backend with Docker on AWS EC2 and Connected it to AWS RDS

Introduction

I recently set up a project where I hosted a Node.js frontend and backend on an AWS EC2 instance using Docker. The backend connected to a MySQL database running on AWS RDS to store data. This setup made deployment smoother and scalable. In this blog, I’ll explain how I did it step by step.

What I Used

  • Frontend & Backend: Node.js with Express.js

  • Database: AWS RDS (MySQL)

  • Hosting: AWS EC2 with Docker

  • Extras: Nginx for handling traffic, Docker Compose for managing containers, and GitHub Actions for CI/CD

Setting Up AWS RDS

Before setting up the EC2 instance, I created a MySQL database using AWS RDS:

  1. Logged into AWS RDS and created a MySQL database.

  2. Chose the Free Tier for cost savings.

  3. Set the security group to allow connections from my EC2 instance.

  4. Created a database and saved my credentials for later.

Deploying the Backend with Docker on AWS EC2

I hosted the backend on an EC2 instance inside a Docker container.

  1. Started an EC2 instance (Ubuntu or Amazon Linux)

  2. Installed Docker and Docker Compose:

  3. Pulled the docker image from the Docker Hub.

     sudo yum install docker -y 
     sudo systemctl start docker
     sudo docker pull philippaul/node-mysql-app:02
    
  4. Run the Image pulled from Docker Hub.

     docker run --rm -p 80:3000 -e DB_HOST="your_hostname" -e DB_User="admin" -e DB_password="password" -d philippaul/node-mysql-app:02
    
  5. We will now check and add the data from MySql as well , we will run the below command.

     sudo docker run -it --rm mysql:8.0 mysql -h "your_host_name" -u admin -p
    
  6. Now we can Run the basics Query of MySql to show the data available.

     select * from contacts;
    

Automating Deployment with GitHub Actions

To make deployments smoother, I used GitHub Actions:

  1. Created a workflow to build and push Docker images.

  2. Configured the EC2 instance to pull updates and restart containers.

  3. Automated deployments whenever I pushed changes to the repository.

Final Thoughts

Hosting a full-stack Node.js application with Docker on EC2 while using AWS RDS for the database made my deployment process efficient and scalable. If you’re working on a similar setup, I hope this guide helps!

0
Subscribe to my newsletter

Read articles from Kunal Singh directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kunal Singh
Kunal Singh