A Flask-based web application with MySQL integration, fully containerized using Docker. This project provides a simple setup to run a Flask application connected to a MySQL database, leveraging Docker Compose for ease of deployment.
- Flask Application: Powered by Python's lightweight Flask framework.
- MySQL Database: Integrated MySQL database for persistent data storage.
- Dockerized Application: Seamless containerization with Docker and Docker Compose.
- Environment-Based Configuration: Centralized configuration using .env file for sensitive data.
- Email Notifications: Integrated SMTP settings for sending emails.
Before you begin, ensure you have the following installed on your machine:
-
Git
Download and install Git: Git Downloads -
Docker
Download and install Docker: Docker Desktop -
Docker Compose
Included with Docker Desktop. Check version with:docker-compose --version
To get started with the project, clone the repository using Git:
git clone https://github.com/riddhigupta1110/first-docker-playground.git
Navigate to the project directory:
cd first-docker-playground
The application relies on environment variables stored in a .env file. Create the .env file in the root directory with the following content:
DB_HOST=mysql
DB_PORT=3306
DB_USER=root
DB_PASSWORD=yourpassword
DB_NAME=testdb
SMTP_SERVER=smtp.gmail.com
SMTP_PORT=465
SMTP_USE_TLS=False
SMTP_USE_SSL=True
SMTP_USER=[email protected]
SMTP_PASSWORD=your-email-password
SMTP_DEFAULT_SENDER=[email protected]
docker-compose up --build
This will build both the Flask and MySQL containers, ensuring that MySQL creates the correct database (testdb). It will also create volumes if they are defined in the docker-compose.yml file and do not already exist. If the volumes are already present, Docker Compose will reuse them.
The command also starts the containers as necessary.
This will list all running containers.
docker ps
If MySQL is running, you can check if the database was created successfully. To do this:
- Access the MySQL container:
docker exec -it mysql-db bash
- Open the MySQL CLI:
mysql -u root -p
- When prompted for the password, enter the password you set in the "MYSQL_ROOT_PASSWORD" environment variable.
- Once logged in, list the databases:
SHOW DATABASES;
Finally, you can try accessing your Flask app by visiting http://localhost:5000/ in your browser.
- Stop and remove the containers:
docker-compose down
- Remove any existing volumes (this will erase all data stored in MySQL):
docker-compose down -v
This command will remove the containers and the associated volumes, so any old database data will be removed, and MySQL will re-initialize next time you use the docker-compose up --build
command.