You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
sudo systemctl start mysql
sudo systemctl enable mysql
Set the MySQL root password:
sudo mysql
ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY 'password';
FLUSH PRIVILEGES;
Create the database, user and table:
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%';
FLUSH PRIVILEGES;
Allow Remote Connections to MySQL
Edit the MySQL configuration file:
sudo vi /etc/mysql/mysql.conf.d/mysqld.cnf
Change it to:
bind-address = 0.0.0.0
Restart MySQL:
sudo systemctl restart mysql
Run Your Flask App
Install the required Python packages:
pip install flask mysql-connector-python werkzeug
Start the Flask application:
python3 app.py
Part-2. Dockerize
Step 1: Create a Docker Network
First, create a custom Docker network to allow communication between the MySQL and Flask containers.
Enter the MySQL container to check if the database was created:
docker exec -it mysql_container mysql -u root -p
Use password when prompted and run:
SHOW DATABASES;
USE mydb;
Step 3: Update Flask App's app.py
db = mysql.connector.connect(
host="mysql_container", # MySQL container's name
user="root",
password="password",
database="mydb"
)
Create Table is not present
CREATE DATABASE mydb;
USE mydb;
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
password VARCHAR(255) NOT NULL
);
CREATE USER 'root'@'%' IDENTIFIED BY 'password';
GRANT ALL PRIVILEGES ON mydb.* TO 'root'@'%';
FLUSH PRIVILEGES;
Step 4: Create Dockerfile for Flask App
FROM python:3.11-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
EXPOSE 5000
CMD ["python3","app.py"]
Step 5: Build and Run the Flask App Container
Build the Flask app image:
docker build -t flask-app .
Run the Flask app container connected to the same Docker network: