This is a simple Flask application 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.
Before you begin, make sure you have the following installed:
- Docker
- Git (optional, for cloning the repository)
Clone this repository to your local machine (if you haven't already)
git clone https://github.com/your-username/your-repo-name.git
Navigate to the project directory:
cd your-repo-name
Create a .env
file in the project directory to store your MySQL environment variables:
touch .env
Open the .env
file and add your MySQL configuration:
MYSQL_HOST=mysql
MYSQL_USER=your_username
MYSQL_PASSWORD=your_password
MYSQL_DB=your_database
Start the containers using Docker Compose:
docker-compose up --build
Once the containers are up and running, access the application in your web browser:
- Frontend: http://localhost
- Backend: http://localhost:5000
Use a MySQL client or tool (e.g., phpMyAdmin) to execute the following SQL commands and create the messages
table in your MySQL database:
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
- Visit http://localhost to see the frontend where you can submit new messages using the form.
- Visit http://localhost:5000/insert_sql to insert a message directly into the messages table via an SQL query.
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
First, create a Docker image from the Dockerfile
:
docker build -t flaskapp .
Create a custom network for the containers to communicate with each other:
docker network create twotier
Run the MySQL container on the custom network:
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
Run the Flask backend container, linking it to the same network:
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
- Make sure to replace the placeholders (
your_username
,your_password
,your_database
) with your actual MySQL configuration in the.env
file. - This setup is a basic demonstration. For production environments, follow best practices for security and performance.
- Be cautious when executing SQL queries directly. Validate and sanitize user inputs to prevent vulnerabilities like SQL injection.
- If you encounter issues, check Docker logs and error messages for troubleshooting.
This version is properly structured, and each section is clearly marked with headings for ease of understanding and navigation.