A small two-tier app: a Flask web app that stores messages in MySQL. Submit a message in the form; it is saved in the database and listed on the page.
Stack: Python 3.13, Flask 3.1, gunicorn, PyMySQL, MySQL 8.4.
| Route | What it does |
|---|---|
GET / |
Lists all messages and shows which server answered |
POST /submit |
Saves new_message (form field), returns it as JSON |
GET /health |
Returns {"status":"ok"} (no database call), used by health checks |
- Docker with the Compose plugin (
docker compose) - Git (optional, for cloning the repository)
-
Clone and enter the repo:
git clone https://github.com/LondheShubham153/two-tier-flask-app.git cd two-tier-flask-app -
(Optional) set your own database credentials. Without a
.envfile the defaults indocker-compose.ymlare used.cp .env.example .env # then edit the passwords -
Start everything:
docker compose up --build
-
Open http://localhost:5000, send a few messages. The
messagestable is created automatically.On macOS, port 5000 may already be used by AirPlay Receiver. Turn it off in System Settings, or change the left side of
"5000:5000"indocker-compose.yml. -
Stop and remove the containers (
-valso deletes the database volume):docker compose down # keep data docker compose down -v # delete data too
make build, make run, make stop, make test and make clean are shortcuts for the common commands.
-
Build the image and create a network:
docker build -t flaskapp . docker network create twotier -
Start MySQL:
docker run -d \ --name mysql \ -v mysql-data:/var/lib/mysql \ --network=twotier \ -e MYSQL_DATABASE=mydb \ -e MYSQL_ROOT_PASSWORD=admin \ -p 3306:3306 \ mysql:8.4 -
Start the app (wait a few seconds for MySQL to be ready first):
docker run -d \ --name flaskapp \ --network=twotier \ -e MYSQL_HOST=mysql \ -e MYSQL_USER=root \ -e MYSQL_PASSWORD=admin \ -e MYSQL_DB=mydb \ -p 5000:5000 \ flaskapp:latest
The app reads its database settings from environment variables:
| Variable | Default |
|---|---|
MYSQL_HOST |
localhost |
MYSQL_PORT |
3306 |
MYSQL_USER |
default_user |
MYSQL_PASSWORD |
default_password |
MYSQL_DB |
default_db |
FLASK_DEBUG |
off (1 enables debug when running python app.py) |
pip install -r requirements-dev.txt
pytest| Path | Purpose |
|---|---|
Dockerfile, Dockerfile-multistage |
Container images (the second shows the multi-stage pattern) |
docker-compose.yml, Makefile |
Local run |
Jenkinsfile |
CI/CD pipeline |
beanstalk/ |
Run the app on AWS Elastic Beanstalk (compose file for Standard Mode, Docker Hub image plus RDS for Cluster Mode) |
k8s/, eks-manifests/ |
Kubernetes manifests (kubeadm cluster / Amazon EKS) |
aws/ |
Step-by-step guide to run this app on AWS: VPC, EC2, RDS, ALB, Auto Scaling, CloudWatch |
- This is a demo setup. For production use real secrets management, TLS and backups.
- If something fails, check
docker compose logs.