# "Two-Tier Web App with Docker: Flask Frontend + MySQL Backend"

## 📘 **Introduction**

In this blog, I’ll walk you through how I deployed a **two-tier Flask web application** with a **MySQL database** using **Docker** containers on an **AWS EC2 instance**. This setup ensures container isolation, environment consistency, and real-time communication between the app and database via Docker networking.

By the end of this guide, your application will be accessible over the internet with just a public IP and a browser!

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text"><strong>🚀🚀 Check out the full project on my </strong><a target="_self" rel="noopener noreferrer nofollow" href="https://github.com/ApurvGujjar07/Two-Tier-Flask-App-.git" style="pointer-events: none">GitHub Repository</a></div>
</div>

## [☁️ **1\. Launching AWS EC2 Instance (Ubuntu)**](https://github.com/gujjarapurv/two-tier-flask-docker)

* [Go](https://github.com/gujjarapurv/two-tier-flask-docker) to AW[S EC2 Console.](https://github.com/gujjarapurv/two-tier-flask-docker)
    
* [Launch a new instance with the fol](https://github.com/gujjarapurv/two-tier-flask-docker)lowing:
    
    * **AMI**: Ubuntu 20.04 or 22.04 LTS
        
    * **Instance Type**: t2.micro (Free Tier)
        
    * **Storage**: 8 GB or more
        
    * **Security Group Rules**:
        
        * SSH (Port 22) – for terminal access
            
        * HTTP (Port 80) – optional
            
        * **Custom TCP (Port 5000)** – to access Flask App
            
* Key pair: Create or use an existing one (e.g., `my-key.pem`)
    

> ✅ Connect to the instance:

```powershell
ssh -i my-key.pem ubuntu@your-public-ip
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751945661664/9d24457c-6a8f-488b-be2b-64a7748a903e.png align="center")

## 🐳 **2\. Installing Docker and Git on Ubuntu**

```powershell
sudo apt update
sudo apt install -y docker.io 
sudo usermod -aG docker $USER && newgrp docker
```

**1\.** `sudo apt update`  
Updates the package list to fetch the latest versions available from repositories.

**2\.** `sudo apt install -y` [`docker.io`](http://docker.io)  
Installs Docker Engine from Ubuntu’s default repository without asking for confirmation.

**3\.** `sudo usermod -aG docker $USER && newgrp docker`  
Adds your user to the Docker group so you can run Docker without `sudo`.

## 🔧 **3\. Cloning the Flask Project**

```powershell
clone https://github.com/ApurvGujjar07/Two-Tier-Flask-App-.git
cd two-tier-flask-app
ls
```

You’ll see files like [`app.py`](http://app.py), `Dockerfile`, etc.

## 📦 **4\. Build Flask App Docker Image**

```powershell
docker build -t two-tier-backend .
```

This command packages the app into a Docker image named `two-tier-backend`.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946195016/27df688c-5725-445d-9546-486e0f839eb0.png align="center")

## 🌐 **5\. Create Docker Network**

```powershell
docker network create mynetwork
```

This allows containers to communicate internally by name (DNS-style linking).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946242612/56109110-d953-4a20-959c-a778b1dfbf3a.png align="center")

## 🐬 **6\. Run MySQL Container**

```powershell
docker run -d --name mysql --network mynetwork \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=devops \
mysql
```

> ✅ MySQL now runs and is ready for connection on the Docker network.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946280314/b7bfb388-c01f-4001-944f-6343bef4b283.png align="center")

## 🚀 **7\. Run Flask App Container**

```powershell
docker run -d -p 5000:5000 --network mynetwork \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=root \
-e MYSQL_DB=devops \
two-tier-backend:latest
```

Your Flask app is now running and mapped to **port 5000** of your EC2 instance.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946355676/4456af58-0a09-481d-bea8-56fa6844e85b.png align="center")

## 🔍 **8\. Verify Everything is Working**

### Check running containers

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946373970/2fff60e2-7058-4b54-aced-7040f9f0d9d7.png align="center")

## 🌐 **9\. Access App in Browser**

* Go to AWS Console → **Security Groups**
    
* **Add inbound rule**:
    
    * Type: Custom TCP
        
    * Port: **5000**
        
    * Source: Anywhere (0.0.0.0/0)
        

Then visit:  
🌍 `http://<your-public-ip>:5000`

> ✅ You should see the live Flask web app now!

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946529474/09a837d1-98f8-4b9a-b7c9-d0340cb1d2fd.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946556646/2372b1de-4bfc-4df3-9a86-3540f58672a8.png align="center")

### Connect to MySQL and verify DB:

```powershell
docker exec -it mysql mysql -u root -p
# Enter: root
```

Then run:

```powershell
SHOW DATABASES;
USE devops;
SHOW TABLES;
SELECT * FROM messages;
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946633049/4a7abbca-4112-4baa-aeba-979fc2286f77.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751946612074/d7bf6f52-e727-4101-bd87-3e7da1ba88be.png align="center")

## 🧠 **Conclusion**

In this blog, I demonstrated how to:

* Launch an EC2 instance
    
* Install and configure Docker
    
* Deploy a two-tier Flask + MySQL app
    
* Connect containers via Docker network
    
* Expose the app publicly via port 5000
    

This is a solid DevOps practice project showing real-world containerization, networking, and deployment skills.

## 👨‍💻 About the Author

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1751797710818/123a7231-3dca-4273-ad68-7bd026f69b95.png align="center")

> Hi, I'm **Apurv Gujjar**, a passionate and aspiring DevOps Engineer.  
> "This project marks the beginning of my journey into the world of DevOps, containerization, and cloud-native application deployment."
