Reading Time: 4 minutes
In this tutorial, I will show you how to use Docker Compose to create a FastAPI application with MySQL, along with PHPMyAdmin and the Traefik proxy manager. Docker Compose is a tool that allows you to define and run multi-container Docker applications.
Step 1: Install Docker and Docker Compose
To use Docker Compose, you first need to have Docker installed on your computer. You can download Docker from the official Docker website (https://www.docker.com/get-started). Additionally, you need to install Docker Compose, which you can download from the official Docker Compose website (https://docs.docker.com/compose/install/).
Step 2: Create the FastAPI Application
To create the FastAPI application that we will use in this tutorial, follow the same steps as in the previous tutorial.
Step 3: Create the docker-compose.yml File
Instead of creating a Dockerfile
for each application container, we will use a docker-compose.yml
file to define the containers. Here’s an example of a docker-compose.yml
file for a FastAPI application with MySQL, PHPMyAdmin, and Traefik:
version: '3.9' services: app: build: . ports: - "80:80" depends_on: - db environment: - MYSQL_USER=root - MYSQL_PASSWORD=example - MYSQL_HOST=db - MYSQL_PORT=3306 - MYSQL_DATABASE=mydatabase networks: - web db: image: mysql:8.0 restart: always environment: - MYSQL_ROOT_PASSWORD=example - MYSQL_DATABASE=mydatabase volumes: - mysql_data:/var/lib/mysql networks: - web phpmyadmin: image: phpmyadmin/phpmyadmin restart: always environment: - PMA_HOST=db - MYSQL_ROOT_PASSWORD=example labels: - "traefik.enable=true" - "traefik.http.routers.phpmyadmin.rule=Host(`phpmyadmin.example.com`)" - "traefik.http.middlewares.phpmyadmin-auth.basicauth.users=admin:$apr1$BXjTBCuD$/XgTlNGcqehJFt78sfl1I/" - "traefik.http.routers.phpmyadmin.middlewares=phpmyadmin-auth" networks: - web traefik: image: traefik:v2.4 restart: always ports: - "80:80" - "443:443" volumes: - /var/run/docker.sock:/var/run/docker.sock:ro - traefik_config:/etc/traefik - traefik_data:/var/lib/traefik networks: - web volumes: mysql_data: traefik_config: traefik_data: networks: web:
This file defines four services: app
, db
, phpmyadmin
, and traefik
.
The app
service uses the Dockerfile
from the current directory and runs on port 80. It depends on the db
service and defines the environment variables to connect to the MySQL database.
The
The phpmyadmin
service uses the PHPMyAdmin image and connects to the db
service. This service runs on port 8080 and defines the host for access through Traefik, as well as basic authentication for username and password.
Finally, the traefik
service uses the Traefik image and runs on ports 80 and 443. It also defines the volume for Traefik configuration and data.
The file also defines three volumes for MySQL and Traefik data, and a network called web
to connect all services.
Step 4: Run the Application with Docker Compose
To run the application with Docker Compose, simply run the following command in the directory where the docker-compose.yml
file is located:
docker-compose up -d
This command will build and run all the containers defined in the docker-compose.yml
file. The -d
option indicates that the containers should run in the background.
Once the containers have been created, you can access the FastAPI application in your web browser at http://localhost
. You can also access PHPMyAdmin at http://phpmyadmin.example.com
(make sure to change phpmyadmin.example.com
to your own domain).
Step 5: Manage the Application with Traefik and Proxy Manager
Traefik is a reverse proxy and load balancer that allows us to easily manage our applications. To use Traefik in our project, we need to add some labels to our docker-compose.yml
file. These labels tell Traefik to route traffic to the phpmyadmin
service on port 8080.
Additionally, we can use Proxy Manager to centrally manage our web applications. Proxy Manager allows us to easily add SSL and basic authentication to our applications. To use Proxy Manager in our project, we need to add some configurations to our docker-compose.yml
file.
Here’s the updated docker-compose.yml
file with Traefik labels and Proxy Manager configurations:
version: '3.9' services: app: build: . ports: - "80:80" environment: - MYSQL_USER=root - MYSQL_PASSWORD=example - MYSQL_HOST=db - MYSQL_PORT=3306 - MYSQL_DATABASE=mydatabase networks: - web labels: - "traefik.enable=true" - "traefik.http.routers.app.rule=Host(`example.com`)" - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https" - "traefik.http.routers.app.middlewares=redirect-to-https" - "traefik.http.routers.app-secure.rule=Host(`example.com`)" - "traefik.http.routers.app-secure.entrypoints=websecure" - "traefik.http.routers.app-secure.tls.certresolver=myresolver" db: image: mysql:8.0 restart: always environment: - MYSQL_ROOT_PASSWORD=example - MYSQL_DATABASE=mydatabase volumes: - mysql_data:/var/lib/mysql networks: - web phpmyadmin: image: phpmyadmin/phpmyadmin restart: always environment: - PMA_HOST=db - PMA_PORT=3306 - PMA_USER=root - PMA_PASSWORD=example - PHP_UPLOAD_MAX_FILESIZE=1G - PHP_MAX_INPUT_VARS=1G labels: - "traefik.enable=true" - "traefik.http.routers.phpmyadmin.rule=Host(phpmyadmin.example.com)" - "traefik.http.middlewares.redirect-to-https.redirectscheme.scheme=https" - "traefik.http.routers.phpmyadmin.middlewares=redirect-to-https" - "traefik.http.routers.phpmyadmin-secure.rule=Host(phpmyadmin.example.com)" - "traefik.http.routers.phpmyadmin-secure.entrypoints=websecure" - "traefik.http.routers.phpmyadmin-secure.tls.certresolver=myresolver" - "traefik.http.services.phpmyadmin.loadbalancer.server.port=80" networks: - web traefik: image: traefik:v2.4 restart: always ports: - "80:80" - "443:443" volumes: - ./traefik:/etc/traefik - /var/run/docker.sock:/var/run/docker.sock labels: - "traefik.enable=true" - "traefik.http.routers.traefik.rule=Host(traefik.example.com)" - "traefik.http.routers.traefik.entrypoints=websecure" - "traefik.http.routers.traefik.tls.certresolver=myresolver" networks: - web volumes: mysql_data: traefik: traefik_data: networks: web: external: true
In the labels
section of the app
service, we have added some labels to define how Traefik should route traffic to the service. We have defined that the Traefik routing rule should be Host(
example.com)
to route traffic to the app
service. We have also added a label to redirect all HTTP traffic to HTTPS.
In the labels
section of the phpmyadmin
service, we have added some labels to define how Traefik should route traffic to the phpmyadmin
service. We have defined that the Traefik routing rule should be Host(
phpmyadmin.example.com)
to route traffic to the phpmyadmin
service. We have also added alabel to redirect all HTTP traffic to HTTPS.
In the labels
section of the traefik
service, we have added some labels to define how Traefik should route traffic to the traefik
service. We have defined that the Traefik routing rule should be Host(
traefik.example.com)
to route traffic to the traefik
service.
In addition, we have added some labels to define basic authentication for the app
and phpmyadmin
services. To do this, you need to add the following volumes to the traefik
service in the docker-compose.yml
file:
volumes: - ./traefik:/etc/traefik - /var/run/docker.sock:/var/run/docker.sock - ./proxy-manager:/data - ./proxy-manager/auth:/auth
Then, create a file called usersfile
inside the auth
directory and add your credentials in the following format:
username:password
Finally, add the following labels in the labels
section of the service you want to protect with basic authentication:
- "traefik.http.middlewares.auth.basicauth.usersfile=/auth/usersfile" - "traefik.http.middlewares.auth.basicauth.removeheader=true" - "traefik.http.routers.app-secure.middlewares=auth" - "traefik.http.routers.phpmyadmin-secure.middlewares=auth"
Now, when you access the app
and phpmyadmin
services, you will be prompted to enter your credentials to log in.
Finally, run the following command to start the services:
docker-compose up -d
Once the services have started successfully, you can access FastAPI at https://example.com
and phpMyAdmin at https://phpmyadmin.example.com
. You can also access Traefik at https://traefik.example.com
.
In conclusion, Docker Compose is a powerful tool for defining and running applications in Docker containers. With this tutorial, you have learned how to create a Docker Compose for a FastAPI application with MySQL, along with phpMyAdmin and Traefik. You have also learned how to add basic authentication to protect your services with a password. We hope this guide has been helpful and that you have been able to create your own Docker Compose for your application.