My DevOps Project: Building a Two-Tier Flask App with MySQL

 After a long journey in DevOps, I’m excited to share my latest project! I’ll be using a GitHub repository to build a two-tier Flask application connected to a MySQL database. Here’s a step-by-step guide on how I set it up.

Project Overview:

In this project, I create a Flask app that takes input from environment variables and stores it in a MySQL database.

GitHub Repository:
Two-Tier Flask App

Step-by-Step Instructions

1. Clone the Repository

git clone https://github.com/LondheShubham153/two-tier-flask-app

2. Build the Docker Image

Navigate to the cloned directory and build the Docker image:

docker build -t two-tier-app:latest .

3. Create a Custom Network

List the existing Docker networks:

docker network ls

Create a custom network named twotier:

docker network create twotier

4. Set Up MySQL

a. Create a Docker Volume:

docker volume ls
docker volume create mysql-data

b. Run the MySQL Container:

docker run -d --name mysql-demo -v mysql-data:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=root mysql:5.7

c. Access the MySQL Container:

docker exec -it mysql-demo bash

d. Log in to MySQL and Create a Database:

mysql -u root -p
# Enter the password: root
CREATE DATABASE devops;
exit
exit

e. Run MySQL with the Custom Network:

docker run -d -v mysql-data:/var/lib/mysql --network twotier --name mysql -e MYSQL_DATABASE=devops -e MYSQL_ROOT_PASSWORD=root mysql:5.7

Explanation:

  • -v mysql-data:/var/lib/mysql: Binds the mysql-data volume to the container's MySQL data directory.
  • --network twotier: Connects the container to the twotier network.
  • --name mysql: Names the container mysql.
  • -e MYSQL_DATABASE=devops -e MYSQL_ROOT_PASSWORD=root: Sets the database name and root password using environment variables.

5. Run the Flask App

Run the Flask Container:

docker run -d --network twotier --name two-tier-app -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=root -e MYSQL_DB=devops -p 5000:5000 two-tier-app:latest

Explanation:

  • --network twotier: Connects the Flask container to the twotier network.
  • Environment variables (MYSQL_HOSTMYSQL_USERMYSQL_PASSWORDMYSQL_DB) pass necessary information for the Flask app to connect to the MySQL database.
  • -p 5000:5000: Exposes port 5000 to access the Flask app.

Here is the YouTube Link: https://youtube.com/@raeesq.?si=v_QK6Q2XXMf9mKep

Conclusion

This project demonstrates how to create a two-tier architecture using Docker, connecting a Flask app with a MySQL database. I hope you find this project helpful and inspiring for your own DevOps journey!

Comments

Popular posts from this blog

📘 Understanding Prometheus in a Simple Way-Part 3 (For DevOps Beginners)

Grafana Setup & Dashboard Creation (Part-5)— Explained by Raees Yaqoob Qazi

My First Python Program: A Simple Calculator