Welcome to the comprehensive guide on deploying a two-tier Flask application using Docker! This tutorial will walk you through every step to ensure a smooth setup.
Let’s get started!
Installation and Setup
1. Launch EC2 instance
2. Install Docker
sudo apt update
sudo apt install docker.io
3. Verify Docker installation by checking active processes:
docker ps
4. Clone the Two-Tier Flask App Repository
https://github.com/sanchit1498/two-tier-flask-app.git
Create and Edit Docker file
1. Create a Dockerfile to build the image for your Flask app:
FROM python:3.9-slim
WORKDIR /app
RUN apt-get update -y \
&& apt-get upgrade -y \
&& apt-get install -y gcc default-libmysqlclient-dev pkg-config \
&& rm -rf /var/lib/apt/lists/*
COPY requirements.txt .
RUN pip install mysqlclient
RUN pip install -r requirements.txt
COPY . .
CMD ["python","app.py"]
2. Build the Docker image for your Flask app:
docker build . -t myflaskapp
3. List Docker images to ensure your image is created:
docker images
4. Create a Docker network for communication between containers:
docker network create twotier
docker network ls
docker network inspect twotier
5. Run MySQL Container with specified environment variables:
docker run -d -p 3306:3306 --network=twotier -e MYSQL_DATABASE=myDb -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_ROOT_PASSWORD=admin --name=mysql mysql:5.7
6. Launch the Flask app container with connections to the MySQL container:
docker run -d -p 5000:5000 --network=twotier -e MYSQL_HOST=mysql -e MYSQL_USER=admin -e MYSQL_PASSWORD=admin -e MYSQL_DB=myDb --name=myflaskapp flaskapp:latest
7. Ensure both containers are running and connected:
docker ps
docker network inspect twotier
Push Flask App Image to Docker Hub
1. Log in to Docker Hub:
docker login
# Enter your ID & Password
2. Tag and push your image:
docker tag flaskapp:latest myflaskapp:latest
docker images
docker push your_user_name/repositry_name:latest
Access Container Shell
docker exec -it bash
Set up the database and table
mysql -u root -p
# Enter your Password
use myDb; # Select database
CREATE TABLE messages (
id INT AUTO_INCREMENT PRIMARY KEY,
message TEXT
);
#create tables in database
Verify the app is running by hitting the following URL in the browser:
URL: server_ip:5000
Docker Compose Configuration
To avoid running two separate commands for each container, you can use Docker Compose to run both simultaneously. To do this, use a text editor to create or modify the Docker Compose configuration file.
1. Install Docker Compose
apt install docker-compose
2. Create the Docker Compose configuration file:
version: '3'
services:
backend:
image: sanchit1498/myflaskapp:latest
ports:
- "5000:5000"
environment:
MYSQL_HOST: "mysql"
MYSQL_USER: "admin"
MYSQL_PASSWORD: "admin"
MYSQL_DB: "myDb"
depends_on:
- mysql
mysql:
image: mysql:5.7
environment:
MYSQL_DATABASE: "myDb"
MYSQL_USER: "admin"
MYSQL_PASSWORD: "admin"
MYSQL_ROOT_PASSWORD: "admin"
ports:
- "3306:3306"
volumes:
- ./message.sql:/docker-entrypoint-initdb.d/message.sql
- mysql-data:/var/lib/mysql
volumes:
mysql-data:
3. Launch Docker Compose:
docker-compose up -d
Conclusion
Congratulations! You’ve successfully set up a two-tier Flask app on Docker. Feel free to explore, modify, and share your experiences with others in the community. Happy coding!