"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."
Subscribe to my newsletter
Read articles from Gujjar Apurv directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Gujjar Apurv
Gujjar Apurv
Gujjar Apurv is a passionate DevOps Engineer in the making, dedicated to automating infrastructure, streamlining software delivery, and building scalable cloud-native systems. With hands-on experience in tools like AWS, Docker, Kubernetes, Jenkins, Git, and Linux, he thrives at the intersection of development and operations. Driven by curiosity and continuous learning, Apurv shares insights, tutorials, and real-world solutions from his journeyโmaking complex tech simple and accessible. Whether it's writing YAML, scripting in Python, or deploying on the cloud, he believes in doing it the right way. "Infrastructure is code, but reliability is art."