Skip to main content

Command Palette

Search for a command to run...

Project Flask App with MySQL Docker Setup

Updated
•6 min read
P

🔹 About Me I am an Engineer with 2+ years of experience in Software,python automation,linux server and Networking at Tata Communications. Currently, I am transitioning my career into DevOps and Site Reliability Engineering (SRE) with hands-on knowledge of Docker, Ansible, Jenkins, Git, CI/CD pipelines, and AWS Cloud. Key contributions include automating data gathering using Python (Selenium), developing and maintaining web portals with Django and PHP, and managing Linux server environments (Red Hat Enterprise Linux 8.7). My networking expertise spans complex troubleshooting and configuration of routers and switches (Juniper, Cisco, Huawei), with hands-on experience in routing protocols (OSPF, BGP), VLANs, MPLS, VPN, and SD-WAN technologies. I am passionate about automation, cloud infrastructure, and reliability engineering, and I am actively seeking DevOps / Site Reliability Engineer opportunities where I can contribute, learn, and grow. 📌 Skills: AWS | Docker | Ansible | Jenkins | Git | CI/CD | Linux | Python | Networking | Fortinet | 📌 Certifications: CCNA Cisco Fortinet Firewall NSE4 AWS Cloud Practitioner Certification Devops skillsup from GFG

Two Tier Application Deployment, we will be dockerizing a two-tier flask application with MySQL using these concepts:

  • Docker Build, Images, Containers

  • Docker Networking

  • Docker Volumes

  • Docker Compose

This is a simple Flask app that interacts with a MySQL database. The app allows users to submit messages, which are then stored in the database and displayed on the frontend.

Prerequisites

Before you begin, make sure you have the following installed:

[ec2-user@ip-172-31-64-194 ~]$ sudo yum install git
[ec2-user@ip-172-31-64-194 ~]$ sudo yum install docker
[ec2-user@ip-172-31-64-194 ~]$ sudo docker login
[ec2-user@ip-172-31-64-194 ~]$ sudo systemctl start docker
[ec2-user@ip-172-31-64-194 ~]$ sudo systemctl enable docker
[ec2-user@ip-172-31-64-194 ~]$ sudo docker ps
CONTAINER ID   IMAGE     COMMAND   CREATED   STATUS    PORTS     NAMES
[ec2-user@ip-172-31-64-194 ~]$ git clone https://github.com/prernask/two-tier-flask-app.git
[ec2-user@ip-172-31-64-194 ~]$ cd two-tier-flask-app
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ ls
Dockerfile             README.md           eks-manifests     templates
Dockerfile-multistage  app.py              k8s
Jenkinsfile            docker-compose.yml  message.sql
Makefile               dummy.txt           requirements.txt
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo rm Dockerfile
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo touch Dockerfile
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo nano Dockerfile
$ sudo docker images
REPOSITORY   TAG       IMAGE ID       CREATED         SIZE
flaskapp     latest    2b8caac2e868   8 seconds ago   495MB
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker run -d -p 5000:5000 flaskapp:latest
8533ca40fb29d8cf10fcb12e1e07e3a2927a4002820751cfcf7c40a744b874a7
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$

Create Dockerfile :

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo cat Dockerfile
FROM python:3.12-slim

WORKDIR /app

# Install mysqlclient dependencies
RUN apt-get update && apt-get install -y \
    gcc \
    default-libmysqlclient-dev \
    pkg-config \
    build-essential \
    && rm -rf /var/lib/apt/lists/*

COPY requirements.txt .

RUN pip install --no-cache-dir -r requirements.txt

COPY . .

CMD ["python", "app.py"]
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker build -t preranakarande/flaskapp:latest .

 sudo docker run -d -p 5000:5000 preranakarande/flaskapp
 sudo docker ps -a
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS                     PORTS                 NAMES
e7ee67871b38   preranakarande/flaskapp   "python app.py"          10 seconds ago   Exited (1) 9 seconds ago                         sweet_mccarthy
a063cb740822   mysql:latest              "docker-entrypoint.s…"   19 minutes ago   Up 19 minutes              3306/tcp, 33060/tcp   some-mysql
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$

]$ sudo docker images
REPOSITORY                TAG       IMAGE ID       CREATED          SIZE
preranakarande/flaskapp   latest    2b8caac2e868   34 minutes ago   495MB
mysql                     latest    f6b0ca07d79d   3 weeks ago      934MB
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker network create mynetwork
95b568215e91d0eaebf25516513e0ab59b86ba5f259675ff8f5dc115f57fffc1
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker network connect mynetwork mysql-db

$ sudo docker run -d -p 5000:5000 preranakarande/flaskapp
f87ccf12a754878e14490b3e868a28d5f96428af74db72c435bf997c1ed386be

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker run -d \
  --name flaskapp \
  --network=mynetwork \
  -p 5000:5000 \
  -e MYSQL_HOST=mysql-db \
  -e MYSQL_USER=root \
  -e MYSQL_PASSWORD=my-secret-pw \
  -e MYSQL_DB=mydb \
  preranakarande/flaskapp
8a539b3db9f1f833c76adbf35fc8cc29c69613167af3a7ed48ed735295587158
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
8a539b3db9f1   preranakarande/flaskapp   "python app.py"          5 seconds ago    Up 4 seconds    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              flaskapp
b24a59208b8c   mysql:latest              "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   33060/tcp, 0.0.0.0:3360->3306/tcp, :::3360->3306/tcp   mysql-db
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ 

sudo docker ps
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
8a539b3db9f1   preranakarande/flaskapp   "python app.py"          5 seconds ago    Up 4 seconds    0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              flaskapp
b24a59208b8c   mysql:latest              "docker-entrypoint.s…"   16 minutes ago   Up 16 minutes   33060/tcp, 0.0.0.0:3360->3306/tcp, :::3360->3306/tcp   mysql-db
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker container start 8a539b3db9f1
8a539b3db9f1
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker network ls
NETWORK ID     NAME        DRIVER    SCOPE
a4b84a42804c   bridge      bridge    local
b2c7456d6807   host        host      local
95b568215e91   mynetwork   bridge    local
fc73b59f1036   none        null      local

MYSQL

 sudo exec -it b24a59208b8c bash
sudo: exec: command not found
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker exec -it b24a59208b8c bash
bash-5.1#
bash-5.1# mysql -u root -p
Enter password:
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
bash-5.1# mysql -u root -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 17
Server version: 9.5.0 MySQL Community Server - GPL

Copyright (c) 2000, 2025, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>
mysql> my db;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'my db' at line 1
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mydb               |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
5 rows in set (0.007 sec)

mysql> use mysql
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> use mydb
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql>
mysql> show tables;
+----------------+
| Tables_in_mydb |
+----------------+
| messages       |
+----------------+
1 row in set (0.006 sec)

mysql> show messages;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'messages' at line 1
mysql> use mydb;
Database changed
mysql> select * from messgaes
    -> ^C
mysql> select * from messages;
+----+---------+
| id | message |
+----+---------+
|  1 | hi      |
|  2 | hello   |
+----+---------+
2 rows in set (0.000 sec)

mysql>
mysql> exit
Bye
bash-5.1# exit
exit
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$
sudo docker push preranakarande/flaskapp:latest

create compose.yml


 sudo yum install docker-compose
Last metadata expiration check: 1:51:43 ago on Sat Nov 15 16:35:02 2025.
No match for argument: docker-compose
Error: Unable to find a match: docker-compose
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo mkdir -p /usr/local/lib/docker/cli-plugins
sudo curl -SL https://github.com/docker/compose/releases/download/v2.27.0/docker-compose-linux-x86_64 \
  -o /usr/local/lib/docker/cli-plugins/docker-compose
sudo chmod +x /usr/local/lib/docker/cli-plugins/docker-compose
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
100 60.0M  100 60.0M    0     0   216M      0 --:--:-- --:--:-- --:--:--  216M
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ docker compose version
Docker Compose version v2.27.0
[ec2-user@ip-172-31-64-194 two-tier-flask-app]

sudo docker container ls
CONTAINER ID   IMAGE                     COMMAND                  CREATED          STATUS          PORTS                                                  NAMES
8a539b3db9f1   preranakarande/flaskapp   "python app.py"          27 minutes ago   Up 27 minutes   0.0.0.0:5000->5000/tcp, :::5000->5000/tcp              flaskapp
b24a59208b8c   mysql:latest              "docker-entrypoint.s…"   44 minutes ago   Up 44 minutes   33060/tcp, 0.0.0.0:3360->3306/tcp, :::3360->3306/tcp   mysql-db

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker container stop 8a539b3db9f1 b24a59208b8c
8a539b3db9f1
b24a59208b8c
[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo nano compose.yml

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo cat compose.yml
version: "3.9"

services:
  mysql-db:
    image: mysql:latest
    container_name: mysql-db
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: my-secret-pw
      MYSQL_DATABASE: mydb
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypass
    ports:
      - "3360:3306"
    volumes:
      - mysql_data:/var/lib/mysql

  flaskapp:
    image: preranakarande/flaskapp
    container_name: flaskapp
    depends_on:
      - mysql-db
    restart: always
    ports:
      - "5000:5000"
    environment:
      MYSQL_HOST: mysql-db
      MYSQL_USER: myuser
      MYSQL_PASSWORD: mypass
      MYSQL_DB: mydb

volumes:
  mysql_data:

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$

[ec2-user@ip-172-31-64-194 two-tier-flask-app]$ sudo docker compose up -d

\============================================================================

1. Check containers

sudo docker ps

Result:

  • flaskapp running on port 5000

  • mysql-db running on port 3360 → 3306


2. Update hosts file

sudo nano /etc/hosts

Added:

127.0.0.1  my-todo-app.com

3. Test the app

curl -L my-todo-app.com:5000

\============================================================================
1. Check containers

sudo docker ps

Result:

  • flaskapp running on port 5000

  • mysql-db running on port 3360 → 3306


2. Update hosts file

sudo nano /etc/hosts

Added:

127.0.0.1  my-todo-app.com

3. Test the app

curl -L my-todo-app.com:5000