
Modern applications are often built using a multi-tier architecture, where the frontend/backend application and the database run independently. This improves scalability, maintainability, and security.
In this project, I deployed a Flask web application and a MySQL database using Docker containers on an AWS EC2 Ubuntu instance.
Modern applications are often built using a multi-tier architecture, where the frontend/backend application and the database run independently. This improves scalability, maintainability, and security.
In this project, I deployed a Flask web application and a MySQL database using Docker containers on an AWS EC2 Ubuntu instance.
Internet
│
▼
AWS EC2 Ubuntu Instance
│
┌─────────────┴─────────────┐
│ │
▼ ▼
Flask Container ───────► MySQL Container │ │ └────────Docker Network─────────┘
Technologies Used
AWS EC2
Ubuntu 22.04
Docker
Docker Network
Docker Volumes
Flask
MySQL
Linux
Git & GitHub
Prerequisites
Before starting, make sure you have:
AWS Account
EC2 Ubuntu Instance
Docker Installed
Git Installed
Port 5000 opened in Security Group
Step 1: Launch an AWS EC2 Instance
Launch an Ubuntu EC2 instance and connect using SSH.
ssh -i key.pem ubuntu@<Public-IP>
Step 2: Update Ubuntu
sudo apt update
sudo apt upgrade -y
Step 3: Install Docker
sudo apt install docker.io -y
Start Docker
sudo systemctl start docker
sudo systemctl enable docker
Verify installation
docker --version
Step 4: Clone the Project
git clone https://github.com/<username>/two-tier-flask-app.git
Go into project directory
cd two-tier-flask-app
Step 5: Build the Flask Docker Image
docker build -t flask-app .
Verify
docker images
Step 6: Create Docker Network
Create a custom bridge network.
docker network create two-tier
Verify
docker network ls
Step 7: Run MySQL Container
docker run -d \
--name mysql \
--network two-tier \
-e MYSQL_ROOT_PASSWORD=root \
-e MYSQL_DATABASE=devops \
mysql:8
Verify
docker ps
Step 8: Run Flask Container
docker run -d \
--name flask-app \
--network two-tier \
-p 5000:5000 \
-e MYSQL_HOST=mysql \
-e MYSQL_USER=root \
-e MYSQL_PASSWORD=root \
-e MYSQL_DB=devops \
flask-app
Step 9: Verify Containers
docker ps
Expected output
CONTAINER ID IMAGE STATUS
xxxxxxxx mysql:8 Up
xxxxxxxx flask-app Up
Step 10: Configure AWS Security Group
Add an inbound rule.
| Type | Port | Source |
|---|---|---|
| Custom TCP | 5000 | 0.0.0.0/0 |
Save the rule.
Step 11: Access the Application
Open the browser
http://<EC2-PUBLIC-IP>:5000
Example
http://54.xxx.xxx.xxx:5000
Docker Networking
The Flask container communicates with MySQL using the container name.
MYSQL_HOST=mysql
Docker DNS automatically resolves the container name.
Docker Volumes
Initially, I did not create a Docker volume manually.
Even though I deleted the MySQL container, the database data still existed.
Why?
The official MySQL Docker image automatically creates an anonymous volume for the /var/lib/mysql directory.
Therefore,
Container deleted ✅
Database data still exists ✅
To check volumes
docker volume ls
Inspect a volume
docker volume inspect <volume-name>
Docker Commands Used
Build Image
docker build -t flask-app .
Create Network
docker network create two-tier
Run MySQL
docker run -d --name mysql --network two-tier -e MYSQL_ROOT_PASSWORD=root -e MYSQL_DATABASE=devops mysql:8
Run Flask
docker run -d --name flask-app --network two-tier -p 5000:5000 -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PA
List Containers
docker ps
List Images
docker images
List Networks
docker network ls
List Volumes
docker volume ls
OUTPUT
Challenges Faced
1. AWS EC2 Capacity Issue
Initially, the EC2 instance failed to start due to insufficient capacity in the selected region.
Solution
Changed the AWS region and relaunched the instance.
2. Docker Network Not Found
Error
network two-tier not found
Solution
Created the custom network.
docker network create two-tier
Initially, the Flask container exited immediately.
Solution
Verified Docker logs.
docker logs flask-app
Corrected the environment variables and restarted the container.
4.Connection Refused
Browser displayed
ERR_CONNECTION_REFUSED
Solution
Opened port 5000 in the AWS Security Group and confirmed the Flask container was running.
5. MySQL Data Persisted
Even after deleting the MySQL container, the data remained.
Reason
Docker automatically created an anonymous volume for the MySQL data directory.
Key Learnings
Building Docker images
Running multiple Docker containers
Docker networking
Container communication
Environment variables
Docker volumes
AWS EC2 deployment
Security Groups
Troubleshooting Docker containers
Container logs and debugging
Conclusion
This project provided hands-on experience with deploying a two-tier web application using Docker on AWS EC2. I learned how containers communicate over Docker networks, how MySQL persists data using Docker volumes, and how to troubleshoot common deployment issues. This project strengthened my understanding of containerization, cloud deployment, and DevOps fundamentals.
About the author
- independent
Comments
Join the discussionNo comments yet. Be the first to comment.




