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.
Before you begin, make sure you have the following installed:
- Docker
- Git (optional, for cloning the repository)
-
Clone this repository (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
-
Access the Flask app in your web browser:
- Frontend: http://localhost
- Backend: http://localhost:5000
-
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 );
-
-
Interact with the app:
- Visit http://localhost to see the frontend. 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 Dockerfile
docker build -t flaskapp .
- Now, make sure that you have created a network using following command
docker network create twotier
- Attach both the containers in the same network, so that they can communicate with each other
i) MySQL container
docker run -d --name mysql -v mysql-data:/var/lib/mysql -v ./message.sql:/docker-entrypoint-initdb.d/message.sql --network=twotier -e MYSQL_DATABASE=mydb -e MYSQL_USER=root -e MYSQL_ROOT_PASSWORD="admin" mysql:5.7
OR
docker run -d --name mysql --network=twotier -e MYSQL_DATABASE=mydb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=test@123 mysql:5.7
OR
docker container run -d --network cricbuzz --name mysql -e MYSQL_ROOT_PASSWORD=test@123 -e MYSQL_DATABASE=cricbuzz -e MYSQL_USER=singh -e MYSQL_PASSWORD=Tom@1234 -v $(pwd)/message.sql:/docker-entrypoint-initdb.d/message.sql -v mysql3-data:/var/lib/mysql mysql:latest
ii) Backend container
docker run -d --name flaskapp -v mysql-data:/var/lib/mysql -v ./message.sql:/docker-entrypoint-initdb.d/message.sql --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=root -e MYSQL_PASSWORD=admin -e MYSQL_DB=mydb -p 5000:5000 flaskapp:latest
OR
docker run -d --name flaskapp --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=mydb -p 5000:5000 flaskapp:latest
OR
docker container run -d --network cricbuzz --name backend -p 5000:5000 -e MYSQL_HOST=mysql -e MYSQL_USER=singh -e MYSQL_PASSWORD=Tom@1234 -e MYSQL_DB=cricbuzz two-tier-app_backend:latest
-
Make sure to replace placeholders (e.g.,
your_username
,your_password
,your_database
) with your actual MySQL configuration. -
This is a basic setup for demonstration purposes. In a production environment, you should 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.