๐ Building a Resilient Flask App with MySQL, Docker, and Docker-Compose๐
๐ Application Overview :
Iโve developed a Flask-based web application that seamlessly interacts with a MySQL database. Users can submit messages through the user-friendly interface, and these messages are securely stored in the MySQL database, then promptly displayed on the frontend.
๐ณ Docker Containerization :
For streamlined packaging and deployment, Iโve containerized this application with Docker. This ensures consistent behavior across different environments and simplifies the deployment process.
git clone https://github.com/rutikdevops/DevOps-Project-5.git
cd DevOps-Project-5
3. Create Docker File :-
vi Dockerfile
# Use an official Python runtime as the base image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# install required packages for system
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y gcc default-libmysqlclient-dev pkg-config \
&& rm -rf /var/lib/apt/lists/*# Copy the requirements file into the container
COPY requirements.txt .# Install app dependencies
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY ..# Specify the command to run your application
CMD ["python", "app.py"]
docker build . -t flaskapp # Create a docker image from Dockerfile
docker images
docker network create twotier # Now, make sure that you have created a network using following command
Attach both the containers in the same network, so that they can communicate with each other
3. Create the messages table in your MySQL database:
docker ps
docker exec -it <paste here mysql container-id> bash
mysql -u root -p
admin # enter password = admin
show databases;
use myDb;# Paste this code in myDb
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
Now run the app in browser :-
To show the data entered in App :-
docker exec -it <paste here mysql container-id> bash
mysql -u root -p
admin # enter password = admin
show databases;
use myDb;select* from messages;# To show the data entered in App
4. Push Docker Image in DockerHub :-
docker login
# Enter username & password
docker tag flaskapp:latest <github_username>/flaskapp:latest
docker images
docker push <github_username>/flaskapp:latest
5. Use docker-compose
Run App using one single command instead of running both the containers one by one :-
apt install docker-compose -y
vi docker-compose.yml
///
version: '3'
services:
backend:
build:
context: .
ports:
- "5000:5000"
environment:
MYSQL_HOST: mysql
MYSQL_USER: admin
MYSQL_PASSWORD: admin
MYSQL_DB: myDb
depends_on:
- mysql
mysql:
image: mysql:5.7
environment:
MYSQL_ROOT_PASSWORD: root
MYSQL_DATABASE: myDb
MYSQL_USER: admin
MYSQL_PASSWORD: admin
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql # Mount sql script into container's /docker-entrypoint-initdb.d directory to get table automatically created
- mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage
volumes:
mysql-data:
///
docker ps
docker kill (paste here container-id)
docker rm (paste here container-id)
docker-compose up -d
# Now your docker-compose app is running