Project-05: Deploying a Flask App with MySQL Using Docker on EC2: Attaching Network and Volume
Running a two-tier application on Docker is a great way to ensure isolation, scalability, and portability for your apps. In this guide, we’ll deploy a Flask app connected to a MySQL database, using Docker containers on an EC2 instance. By the end, you’ll be able to access your Flask app via your public IP.
Prerequisites
Before we begin, ensure:
Docker is installed on your EC2 instance.
Port 5000 is open in your EC2 security group to access the Flask app.
Step 1: Write a Dockerfile for the Flask Application
The first step is to create a Dockerfile that sets up the Flask app, including all dependencies.
Dockerfile
# Use an official Python runtime as the base image
FROM python:3.9-slim
# Set the working directory in the container
WORKDIR /app
# Install required system packages
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y gcc default-libmysqlclient-dev pkg-config \
&& rm -rf /var/lib/apt/lists/*
# Copy the requirements file into the container
COPY requirements.txt .
# Install app dependencies
RUN pip install mysqlclient
RUN pip install --no-cache-dir -r requirements.txt
# Copy the rest of the application code
COPY . .
# Expose port 5000 for Flask
#EXPOSE 5000
# Specify the command to run your application
CMD ["python", "app.py"]
This Dockerfile installs necessary dependencies, such as mysqlclient
, and sets up the Flask app.
Step 2: Build the Docker Image for the Flask App
With the Dockerfile ready, build the Docker image for the Flask application:
docker build -t flask-app .
This command tags the image as flask-app
, which you can verify by running docker images
.
Step 3: Create a Docker Network and Volume
To allow communication between the containers, create a custom network. We’ll also create a Docker volume to persist MySQL data.
docker network create flask-mysql-net
docker volume create mysql-data
flask-mysql-net
is the network that will connect both containers.mysql-data
is the volume that will store MySQL data, preserving it even if the container stops or is removed.
Step 4: Run the MySQL Container with Network and Volume Attached
Now, start the MySQL container, attaching the flask-mysql-net
network and mysql-data
volume directly:
docker run --name mysql-container --network flask-mysql-net \
-v mysql-data:/var/lib/mysql \
-e MYSQL_ROOT_PASSWORD=admin \
-e MYSQL_DATABASE=mydb \
-d mysql:5.7
In this command:
--network flask-mysql-net
connects the container to theflask-mysql-net
network.-v mysql-data:/var/lib/mysql
mounts themysql-data
volume to MySQL’s data directory (/var/lib/mysql
), ensuring data persistence.-e MYSQL_ROOT_PASSWORD=
admin and-e MYSQL_DATABASE=mydb
set up the root password and initial database.
Step 5: Run the Flask App Container with Network Attached
Next, run the Flask container and connect it to the flask-mysql-net
network, passing the MySQL host as an environment variable.
docker run -p 5000:5000 --name flask-app --network flask-mysql-net \
-e DATABASE_HOST=mysql-container flask-app
This command:
Maps port 5000 of the container to port 5000 on the EC2 instance, allowing external access.
Connects the Flask container to the
flask-mysql-net
network.Sets the
DATABASE_HOST
environment variable tomysql-container
, enabling Flask to communicate with MySQL.
Step 6: Open Port 5000 on EC2 Security Group
To access the Flask app from your browser, open port 5000 in the EC2 security group:
Go to the Security Groups section in your AWS EC2 dashboard.
Edit the Inbound Rules to allow HTTP traffic on port 5000.
With this configuration, you can now access your Flask app via http://your-ec2-public-ip:5000
.
Step 7: Verify Data in the MySQL Container
To ensure the connection is successful and data is correctly stored, access the MySQL container and query the database.
Start an interactive session in the MySQL container:
docker exec -it mysql-container bash
Enter the MySQL CLI:
mysql -u root -p
After entering the password, list the databases and select the
flaskdb
database to check data:SHOW DATABASES; USE flaskdb; SELECT * FROM messages;
Access Your Flask App
After setting everything up and opening the necessary port, you can access your application in a web browser using http://your-ec2-public-ip:5000
.
Subscribe to my newsletter
Read articles from Pakeeza Saeed directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by