# Aws RDS with Docker and EC2

# 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.
    
5. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741766389766/9d568e54-77be-499d-8aa6-f2891341e657.png align="center")
    

## 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)
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741766430231/aaff5ffa-99c4-4c30-bb1c-02e415f7a14b.png align="center")
    
2. Installed **Docker** and **Docker Compose**:
    
3. Pulled the docker image from the Docker Hub.
    
    ```plaintext
    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.
    
    ```plaintext
    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. ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741766337096/d25865f9-3d1c-482c-9452-2ca2a1dbaa7b.png align="center")
    
6. We will now check and add the data from MySql as well , we will run the below command.
    
    ```plaintext
    sudo docker run -it --rm mysql:8.0 mysql -h "your_host_name" -u admin -p 
    ```
    
7. Now we can Run the basics Query of MySql to show the data available.
    
    ```plaintext
    select * from contacts;
    ```
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1741766482051/6cb95ece-96bb-43c4-b01a-dd8cfc083feb.png align="center")

## 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!
