Flask + MySQL Docker Project Deploy on EC2 with Terraform
This is a 2-tier application Flask MySQL Docker Project architecture using Docker, Terraform, Git, and GitHub, where
- The Frontend-Tier is a Flask web app.
- The Backend-Tier is a MySQL database.
Flask MySQL Docker Project Each tier is deployed in its own container and connected via a Docker Network.
This real-world application design setup runs on an AWS EC2 instance.
Flask MySQL Docker Project Project Overview
This Flask MySQL Docker Project demonstrates how to:
- Dockerize a Flask application and MySQL database
- Connect them using a custom Docker network
- Run the setup without using docker-compose
- Deploy and test everything on an EC2 instance (Free Tier friendly)
Flask MySQL Docker Project Architecture
Type: Flask MySQL Docker Project 2-Tier Architecture

ALSO READ:
Reusable Terraform Modules Made My AWS EC2 Setup Super Easy
Effortless AWS EC2 Deployment with Terraform: Automate Your Infrastructure Today!
Effortlessly Set Up AWS VPC & Launch an EC2 Instance β A Step-by-Step Guide
Project Folder Structure
flask-mysql-project/
β
βββ app/
β βββ Dockerfile
β βββ app.py
β βββ requirements.txt
β
βββ mysql/
β βββ Dockerfile
β
βββ README.md
Flask App:app/app.py
from flask import Flask, render_template
import mysql.connector
app = Flask(__name__)
def get_db_connection():
connection = mysql.connector.connect(
host="mysql", # β
Corrected from 'mysql-db'
user="root",
password="example",
database="test_db"
)
return connection
@app.route('/')
def hello_world():
connection = get_db_connection()
cursor = connection.cursor()
cursor.execute("SELECT 'Hello, Docker! This is - Tech Base Hub'")
result = cursor.fetchone()
connection.close()
return render_template('index.html', message=result[0])
if __name__ == "__main__":
app.run(host='0.0.0.0')
Flask Dockerfile:app/Dockerfile
FROM python:3.9-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
ENV FLASK_ENV=development
EXPOSE 5000
CMD ["python", "app.py"]
MySQL Dockerfile:mysql/Dockerfile
FROM mysql:5.7
ENV MYSQL_ROOT_PASSWORD=example
ENV MYSQL_DATABASE=test_db
EXPOSE 3306
Flask App
:app/requirements.txt
flask
mysql-connector-python
Deployment on EC2 Install Docker (Via Terraform)
ec2/provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.91.0"
}
}
}
provider "aws" {
# Configuration options
region = "us-east-1"
}
Deployment on EC2 (Via Terraform)
ec2/ec2.tf
resource "aws_instance" "instance" {
ami = "ami-09c813fb71547fc4f"
vpc_security_group_ids = [aws_security_group.allow_tl.id]
instance_type = "t3.micro"
# 20GB is not enough
# root_block_device {
# volume_size = 50 # Set root volume size to 50GB
# volume_type = "gp3" # Use gp3 for better performance (optional)
# }
user_data = file("C:/Devsecops/repos/ec2/installdocker.sh")
tags = {
Name = "docker"
Purpose = "terraform-sample"
ENV = "test"
project = "techbasehub"
}
}
resource "aws_security_group" "allow_tl" {
name = "allow_tl"
description = "Allow TLS inbound traffic and all outbound traffic"
ingress {
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
tags = {
Name = "allow_tl"
}
}
Install Docker shell script
#!/bin/bash
# growpart /dev/nvme0n1 4
# lvextend -l +50%FREE /dev/RootVG/rootVol
# lvextend -l +50%FREE /dev/RootVG/varVol
# xfs_growfs /
# xfs_growfs /var
sudo dnf -y install dnf-plugins-core
sudo dnf config-manager --add-repo https://download.docker.com/linux/rhel/docker-ce.repo
sudo dnf install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
sudo systemctl start docker
sudo systemctl enable docker
sudo usermod -aG docker ec2-user
echo "User added to docker group"
sudo reboot # (Optional)
Clone your GitHub repo:
git clone https://github.com/krishnatummeti/dockerfile.git
Run the Project:
Create a Docker network
docker network create flask-network
Run MySQL container:
cd mysql
docker build -t mysql:1.0.0 .
docker run -d --name mysql --network flask-network mysql:1.0.0
Run Flask App container:
cd ../app
docker build -t flaskapp:1.0.0 .
docker run -d --name flaskapp -p 5000:5000 --network flask-network flaskapp:1.0.0
π Test the App
http://3.91.12.159:5000/
