Skip to main content

Command Palette

Search for a command to run...

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

Updated
β€’3 min read
"Two-Tier Web App with Docker: Flask Frontend + MySQL Backend"
G

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

πŸ“˜ 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!

πŸ’‘
πŸš€πŸš€ Check out the full project on my GitHub Repository

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

More from this blog

T

The OpsVerse with Apurv

26 posts

Sharing hands-on DevOps, AWS, and Cloud tutorials with real-world projects, tips, and automation guides for students and professionals.