"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!
βοΈ 1. Launching AWS EC2 Instance (Ubuntu)
Go to AWS EC2 Console.
Launch a new instance with the following:
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:
ssh -i my-key.pem ubuntu@your-public-ip

π³ 2. Installing Docker and Git on Ubuntu
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
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
clone https://github.com/ApurvGujjar07/Two-Tier-Flask-App-.git
cd two-tier-flask-app
ls
Youβll see files like app.py, Dockerfile, etc.
π¦ 4. Build Flask App Docker Image
docker build -t two-tier-backend .
This command packages the app into a Docker image named two-tier-backend.

π 5. Create Docker Network
docker network create mynetwork
This allows containers to communicate internally by name (DNS-style linking).

π¬ 6. Run MySQL Container
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.

π 7. Run Flask App Container
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.

π 8. Verify Everything is Working
Check running containers

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


Connect to MySQL and verify DB:
docker exec -it mysql mysql -u root -p
# Enter: root
Then run:
SHOW DATABASES;
USE devops;
SHOW TABLES;
SELECT * FROM messages;


π§ 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

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






