Skip to content

Commit b656793

Browse files
author
Ubuntu
committed
added 2 tier flask app
0 parents  commit b656793

File tree

6 files changed

+205
-0
lines changed

6 files changed

+205
-0
lines changed

Dockerfile

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use an official Python runtime as the base image
2+
FROM python:3.9-slim
3+
4+
# Set the working directory in the container
5+
WORKDIR /app
6+
7+
# install required packages for system
8+
RUN apt-get update \
9+
&& apt-get upgrade -y \
10+
&& apt-get install -y gcc default-libmysqlclient-dev pkg-config \
11+
&& rm -rf /var/lib/apt/lists/*
12+
13+
# Copy the requirements file into the container
14+
COPY requirements.txt .
15+
16+
# Install app dependencies
17+
RUN pip install mysqlclient
18+
RUN pip install --no-cache-dir -r requirements.txt
19+
20+
# Copy the rest of the application code
21+
COPY . .
22+
23+
# Specify the command to run your application
24+
CMD ["python", "app.py"]
25+

README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
2+
# Flask App with MySQL Docker Setup
3+
4+
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.
5+
6+
## Prerequisites
7+
8+
Before you begin, make sure you have the following installed:
9+
10+
- Docker
11+
- Git (optional, for cloning the repository)
12+
13+
## Setup
14+
15+
1. Clone this repository (if you haven't already):
16+
17+
```bash
18+
git clone https://github.com/your-username/your-repo-name.git
19+
```
20+
21+
2. Navigate to the project directory:
22+
23+
```bash
24+
cd your-repo-name
25+
```
26+
27+
3. Create a `.env` file in the project directory to store your MySQL environment variables:
28+
29+
```bash
30+
touch .env
31+
```
32+
33+
4. Open the `.env` file and add your MySQL configuration:
34+
35+
```
36+
MYSQL_HOST=mysql
37+
MYSQL_USER=your_username
38+
MYSQL_PASSWORD=your_password
39+
MYSQL_DB=your_database
40+
```
41+
42+
## Usage
43+
44+
1. Start the containers using Docker Compose:
45+
46+
```bash
47+
docker-compose up --build
48+
```
49+
50+
2. Access the Flask app in your web browser:
51+
52+
- Frontend: http://localhost
53+
- Backend: http://localhost:5000
54+
55+
3. Create the `messages` table in your MySQL database:
56+
57+
- Use a MySQL client or tool (e.g., phpMyAdmin) to execute the following SQL commands:
58+
59+
```sql
60+
CREATE TABLE messages (
61+
id INT AUTO_INCREMENT PRIMARY KEY,
62+
message TEXT
63+
);
64+
```
65+
66+
4. Interact with the app:
67+
68+
- Visit http://localhost to see the frontend. You can submit new messages using the form.
69+
- Visit http://localhost:5000/insert_sql to insert a message directly into the `messages` table via an SQL query.
70+
71+
## Cleaning Up
72+
73+
To stop and remove the Docker containers, press `Ctrl+C` in the terminal where the containers are running, or use the following command:
74+
75+
```bash
76+
docker-compose down
77+
```
78+
79+
## Notes
80+
81+
- Make sure to replace placeholders (e.g., `your_username`, `your_password`, `your_database`) with your actual MySQL configuration.
82+
83+
- This is a basic setup for demonstration purposes. In a production environment, you should follow best practices for security and performance.
84+
85+
- Be cautious when executing SQL queries directly. Validate and sanitize user inputs to prevent vulnerabilities like SQL injection.
86+
87+
- If you encounter issues, check Docker logs and error messages for troubleshooting.
88+
89+
```
90+

app.py

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import os
2+
from flask import Flask, render_template, request, redirect, url_for
3+
from flask_mysqldb import MySQL
4+
5+
app = Flask(__name__)
6+
7+
# Configure MySQL from environment variables
8+
app.config['MYSQL_HOST'] = os.environ.get('MYSQL_HOST', 'localhost')
9+
app.config['MYSQL_USER'] = os.environ.get('MYSQL_USER', 'default_user')
10+
app.config['MYSQL_PASSWORD'] = os.environ.get('MYSQL_PASSWORD', 'default_password')
11+
app.config['MYSQL_DB'] = os.environ.get('MYSQL_DB', 'default_db')
12+
13+
# Initialize MySQL
14+
mysql = MySQL(app)
15+
16+
@app.route('/')
17+
def hello():
18+
cur = mysql.connection.cursor()
19+
cur.execute('SELECT message FROM messages')
20+
messages = cur.fetchall()
21+
cur.close()
22+
return render_template('index.html', messages=messages)
23+
24+
@app.route('/submit', methods=['POST'])
25+
def submit():
26+
new_message = request.form.get('new_message')
27+
cur = mysql.connection.cursor()
28+
cur.execute('INSERT INTO messages (message) VALUES (%s)', [new_message])
29+
mysql.connection.commit()
30+
cur.close()
31+
return redirect(url_for('hello'))
32+
33+
if __name__ == '__main__':
34+
app.run(host='0.0.0.0', port=5000, debug=True)
35+

docker-compose.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
version: '3'
2+
services:
3+
4+
backend:
5+
build:
6+
context: .
7+
ports:
8+
- "5000:5000"
9+
environment:
10+
MYSQL_HOST: mysql
11+
MYSQL_USER: your_username
12+
MYSQL_PASSWORD: your_password
13+
MYSQL_DB: your_database
14+
depends_on:
15+
- mysql
16+
17+
mysql:
18+
image: mysql:5.7
19+
environment:
20+
MYSQL_ROOT_PASSWORD: your_root_password
21+
MYSQL_DATABASE: your_database
22+
MYSQL_USER: your_username
23+
MYSQL_PASSWORD: your_password
24+
volumes:
25+
- mysql-data:/var/lib/mysql # Mount the volume for MySQL data storage
26+
27+
volumes:
28+
mysql-data:

requirements.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Flask==2.0.1
2+
Flask-MySQLdb==0.2.0
3+
requests==2.26.0
4+

templates/index.html

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<title>Hello Flask Frontend with MySQL</title>
5+
<style>
6+
/* ... (your CSS styles) */
7+
</style>
8+
</head>
9+
<body>
10+
<div class="container">
11+
<h1>Hello, Flask Frontend with MySQL!</h1>
12+
{% for message in messages %}
13+
<p>{{ message[0] }}</p>
14+
{% endfor %}
15+
16+
<form action="/submit" method="post">
17+
<input type="text" name="new_message" placeholder="Enter a new message">
18+
<input type="submit" value="Submit">
19+
</form>
20+
</div>
21+
</body>
22+
</html>
23+

0 commit comments

Comments
 (0)