Deploying a Two-Tier Flask App with MySQL Using Docker: A Comprehensive Guide
Table of contents
- Introduction
- Prerequisites ๐ ๏ธ
- Step 1: Clone the Repository ๐งโ๐ป
- Step 2: Build the Flask App Image ๐ผ๏ธ
- Step 3: Create Docker Network ๐
- Step 4: Create Docker Volume ๐พ
- Step 5: Run MySQL Container ๐ข
- Step 6: Run Flask App Container ๐โโ๏ธ
- Step 7: Access MySQL Container ๐ข๏ธ
- Step 8: Add Data to Flask App ๐ฌ
- Step 9: Restart MySQL Container ๐
- Simplify Deployment with Docker Compose ๐ข
- Conclusion ๐
Introduction
DevOps practices have transformed the landscape of software development, emphasizing collaboration and automation. In this tutorial, we'll embark on a journey to deploy a two-tier Flask app with a MySQL database using Docker. We'll focus on key concepts such as Docker volumes and networks to ensure data persistence and efficient communication between containers.
Prerequisites ๐ ๏ธ
Before we begin, ensure you have Docker installed on your machine. You can download Docker here.
Step 1: Clone the Repository ๐งโ๐ป
Start by cloning the project repository from GitHub:
bashCopy codegit clone https://github.com/SandhyaDeotare26/two-tier-flask-app.git
cd two-tier-flask-app
Step 2: Build the Flask App Image ๐ผ๏ธ
Navigate to the Flask app directory and build the Docker image using the provided Dockerfile:
bashCopy codedocker build -t flask-app .
Step 3: Create Docker Network ๐
Create a Docker network to facilitate communication between the Flask app and MySQL containers:
bashCopy codedocker network create -d bridge two-tier-app-nw
Step 4: Create Docker Volume ๐พ
Create a Docker volume to ensure persistent storage for the MySQL database:
bashCopy codedocker volume create --name two-tier-app-volume --opt type=none --opt device=/home/ubuntu/volumes/two-tier-app --opt o=bind
Step 5: Run MySQL Container ๐ข
Start the MySQL container with the following command:
bashCopy codedocker run -d -p 3306:3306 -v two-tier-app-volume:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=test@123 -e MYSQL_DATABASE=testdb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin --network=two-tier-app-nw --name mysql mysql:latest
Step 6: Run Flask App Container ๐โโ๏ธ
Run the Flask app container, ensuring it connects to the created network:
bashCopy codedocker run -d -p 5000:5000 -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=testdb --network=two-tier-app-nw --name flask-app flask-app:latest
Step 7: Access MySQL Container ๐ข๏ธ
Access the MySQL container to create a table inside the testdb database:
bashCopy codedocker exec -it CONTAINER_ID bash
Inside the container, run the following MySQL query to create a table:
sqlCopy codeCREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
Exit the container.
Step 8: Add Data to Flask App ๐ฌ
With the Flask app running, access it through your browser and add some data.
Step 9: Restart MySQL Container ๐
Stop and remove the MySQL container:
bashCopy codedocker stop mysql
docker rm mysql
Then, restart the MySQL container to demonstrate data persistence:
bashCopy codedocker run -d -p 3306:3306 -v two-tier-app-volume:/var/lib/mysql/ -e MYSQL_ROOT_PASSWORD=test@123 -e MYSQL_DATABASE=testdb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin --network=two-tier-app-nw --name mysql mysql:latest
Simplify Deployment with Docker Compose ๐ข
Now, let's streamline the entire deployment process with Docker Compose.
Docker Compose Configuration
Create a docker-compose.yml
file in your project directory with the following content:
yamlCopy codeversion: '3'
services:
mysql:
image: mysql:latest
container_name: mysql
ports:
- "3306:3306"
volumes:
- two-tier-app-volume:/var/lib/mysql/
environment:
MYSQL_ROOT_PASSWORD: test@123
MYSQL_DATABASE: testdb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
networks:
- two-tier-app-nw
flask-app:
image: flask-app:latest
container_name: flask-app
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: testdb
networks:
- two-tier-app-nw
volumes:
two-tier-app-volume:
external: true
networks:
two-tier-app-nw:
driver: bridge
Deploy with Docker Compose
With the docker-compose.yml
file in place, deploying the entire stack becomes a breeze. Run the following command:
bashCopy codedocker-compose up -d
Docker Compose will orchestrate the deployment of both the MySQL and Flask app containers, ensuring seamless communication between them.
Additional Commands
To stop and remove the containers created by Docker Compose, use:
bashCopy codedocker-compose down
Conclusion ๐
Congratulations! You have successfully deployed a two-tier Flask app with a MySQL database using Docker. This tutorial covered essential DevOps practices, including Docker volumes and networks, ensuring seamless container orchestration and data persistence. Feel free to explore and extend this project based on your requirements.
Happy Learning! ๐