Flask + MySQL + Docker: A Trio for Successful Web App Deployment

Β·

3 min read

Flask + MySQL + Docker: A Trio for Successful Web App Deployment

Author: Ujwal Pachghare ⭐

IMPORTANT POINTS

πŸ’‘
These are some important points you must keep in mind while deploying two- or three-tier applications with Docker.
  1. We can use this environment variableMYSQL_ROOT_PASSWORD_FILE=/run/secrets/mysql-rootto retrieve secrets from a file while executing thedocker runcommand.

  2. To run the MySQL database container, we must provide at least the root password and database as environment variables:MYSQL_ROOT_PASSWORD="<root-user-pass>", MYSQL_DATABASE="<db-name>".

  3. Always use the filename you want to mount after the entrypoint directory:./<file>.sql:/docker-entrypoint-initdb.d/<file>.sql. Otherwise, your file will not execute.

  4. Files in this directory execute only once, meaning they will not execute even if you restart your container. They execute only for initializing the container the first time. If you want to execute it again, you have to delete and then run the container again.

  5. Use this environment variableMYSQL_HOST="<db-con-name>"to connect the backend container with the database container (for example, MySQL).

  6. In both the backend/app container and the database container, these environment variable values must be the same:MYSQL_USER="<user>", MYSQL_DATABASE="<db-name>".

  7. Both the backend/app and database containers must be in the same network. So, connect both containers in the same network with this command:--net <net-name>.

Introduction

In this blog post, we will guide you through the process of deploying a simple Flask web application that interacts with a MySQL database using Docker. Docker is a containerization platform that allows us to package and deploy applications in a consistent and isolated environment.

Prerequisites

Before you begin, you will need the following:

  • Docker installed on your system
  • Git (optional, for cloning the project)

Project Setup

1. Clone the Project Repository (Optional)

If you have Git installed, you can clone the project repository to your local machine using the following command:

git clone https://gitlab.com/Aut0ps/flask-mysql-todo-app.git

2. Build the Docker Image

Run the following command to build the Docker image:

docker build -t flask-mysql .

3. Create a Network

docker network create net

4. Run the MySQL Container

docker run -d --name mysql --net net -e MYSQL_ROOT_PASSWORD="admin" -e MYSQL_DATABASE="myDb" -v ./message.sql:/docker-entrypoint-initdb.d/message.sql -p 3306:3306 --rm mysql:5.7

5. Run the Flask Container

docker run -d --name flask --net net -e MYSQL_DB="myDb" -e MYSQL_USER="admin" -e MYSQL_HOST="mysql" -e MYSQL_PASSWORD="admin"  -p 5000:5000 flask-mysql:latest

Usage

1. Access the Flask App

Access the Flask app by visiting http://localhost:5000 in your web browser.

2. Create the messages Table

Execute the following SQL commands using a MySQL client or tool (e.g., phpMyAdmin):

CREATE TABLE messages (
       id INT AUTO_INCREMENT PRIMARY KEY,
       message TEXT
   );

3. Interact with the App

  • Submit new messages using the form on the frontend.

  • View the submitted messages on the backend.

Conclusion

In this blog post, we demonstrate how to deploy a Flask web application with a MySQL database using Docker. This approach allows us to isolate and manage our application and its dependencies in a consistent and portable way. By following the steps outlined above, you can easily deploy and run your own Flask app with MySQL on Docker.


If you find this blog enjoyable, please show your appreciation with some claps and stay connected by subscribing to our newsletter. This way, you won’t miss any updates from AutOps. Thank you for reading...πŸ™πŸ™πŸ™

Did you find this article valuable?

Support AutOps by becoming a sponsor. Any amount is appreciated!

Β