LondheShubham153 / two-tier-flask-app

123 stars 1.38k forks source link

Flask App with MySQL Docker Setup

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:

Setup

  1. Clone this repository (if you haven't already):

    git clone https://github.com/your-username/your-repo-name.git
  2. Navigate to the project directory:

    cd your-repo-name
  3. Create a .env file in the project directory to store your MySQL environment variables:

    touch .env
  4. Open the .env file and add your MySQL configuration:

    MYSQL_HOST=mysql
    MYSQL_USER=your_username
    MYSQL_PASSWORD=your_password
    MYSQL_DB=your_database

Usage

  1. Start the containers using Docker Compose:

    docker-compose up --build
  2. Access the Flask app in your web browser:

  3. Create the messages table in your MySQL database:

    • Use a MySQL client or tool (e.g., phpMyAdmin) to execute the following SQL commands:

      CREATE TABLE messages (
       id INT AUTO_INCREMENT PRIMARY KEY,
       message TEXT
      );
  4. Interact with the app:

Cleaning Up

To stop and remove the Docker containers, press Ctrl+C in the terminal where the containers are running, or use the following command:

docker-compose down

To run this two-tier application using without docker-compose

i) MySQL container

docker run -d \
    --name mysql \
    -v mysql-data:/var/lib/mysql \
    --network=twotier \
    -e MYSQL_DATABASE=mydb \
    -e MYSQL_ROOT_PASSWORD=admin \
    -p 3306:3306 \
    mysql:5.7

ii) Backend container

docker run -d \
    --name flaskapp \
    --network=twotier \
    -e MYSQL_HOST=mysql \
    -e MYSQL_USER=root \
    -e MYSQL_PASSWORD=admin \
    -e MYSQL_DB=mydb \
    -p 5000:5000 \
    flaskapp:latest

Notes