Docker Compose: NGINX PROXY MANAGER + PHPMYADMIN + MYSQL + FASTAPI (with Python) REST Service

Tiempo de lectura: 2 minutos

Reading time: 3 minutes

In this tutorial, we will show you how to create a Docker container with NGINX Proxy Manager, phpMyAdmin, MySQL, and FastAPI (with Python) using Docker Compose. This will allow you to have a complete stack for web development and API in a single container.

Prerequisites

Before getting started, you will need to have Docker and Docker Compose installed on your machine. If you haven’t done so already, follow these steps:

  1. Download and install Docker Desktop from the official website: https://www.docker.com/products/docker-desktop
  2. Verify that Docker has been installed successfully by running the following command in the terminal:
docker --version

You should see the version of Docker that you have installed.

Install Docker Compose by following the instructions in the official documentation: https://docs.docker.com/compose/install/

Verify that Docker Compose has been installed successfully by running the following command in the terminal:

  1. docker-compose --version You should see the version of Docker Compose that you have installed.

Step 1: Create the docker-compose.yml File

The first thing we need to do is create a docker-compose.yml file in a new directory. You can do this by running the following command in the terminal:

mkdir myapp && cd myapp
touch docker-compose.yml

Then, open the docker-compose.yml file in your favorite text editor and add the following content:

version: "3.8"
services:
  proxy:
    image: jlesage/nginx-proxy-manager:latest
    container_name: proxy
    restart: always
    ports:
      - "80:80"
      - "443:443"
      - "81:81"
    environment:
      - TZ=America/New_York
      - PUID=1000
      - PGID=1000
    volumes:
      - ./data:/config
      - ./letsencrypt:/etc/letsencrypt
      - ./nginx.tmpl:/app/nginx.tmpl:ro
  phpmyadmin:
    image: phpmyadmin/phpmyadmin:latest
    container_name: phpmyadmin
    restart: always
    ports:
      - "8080:80"
    environment:
      - PMA_HOST=mysql
      - PMA_USER=root
      - PMA_PASSWORD=my-secret-pw
  mysql:
    image: mysql:8
    container_name: mysql
    restart: always
    environment:
      - MYSQL_ROOT_PASSWORD=my-secret-pw
    volumes:
      - mysql-data:/var/lib/mysql
  app:
    build: ./app
    container_name: app
    restart: always
    ports:
      - "8000:8000"
    environment:
      - DATABASE_URL=mysql+pymysql://root:my-secret-pw@mysql:3306/mydatabase
    depends_on:
      - mysql
volumes:
  mysql-data:

This file defines four services:

  1. proxy: uses the NGINX Proxy Manager image to handle reverse proxy management and SSL certificate management.
  2. phpmyadmin: uses the phpMyAdmin image to provide a user-friendly graphical interface for MySQL.
  3. mysql: uses the official MySQL image for the database server.
  4. app: builds the FastAPI application image using the Dockerfile located in the ./app directory.

Step 2: Create the FastAPI Application

Now we need to create the FastAPI application. In this tutorial, we will use a simple example that demonstrates how to connect to the MySQL database.

First, create a new app directory inside the myapp directory. Then, create a app/main.py file with the following content:

FROM python:3.9

WORKDIR /app

COPY requirements.txt ./
RUN pip install -r requirements.txt

COPY ./app .

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

This file defines the Docker image for our FastAPI application, which uses Python 3.9 and runs on port 8000.

Finally, create a app/requirements.txt file with the following content:

fastapi
uvicorn
SQLAlchemy
pymysql

This file defines the dependencies of our application, including FastAPI, Uvicorn, SQLAlchemy, and PyMySQL.

Step 3: Run the Docker Container

Now we are ready to run the Docker container using Docker Compose.

To do this, make sure you are in the myapp directory and run the following command in the terminal:

docker-compose up -d

This command will start the four services defined in the docker-compose.yml file in detached mode, which means they will run in the background.

Once the containers are up and running, you can access the following URLs from your web browser:

Conclusion

In this tutorial, you learned how to create a Docker container that includes NGINX Proxy Manager, phpMyAdmin, MySQL, and FastAPI. Using Docker Compose, you were able to define and run all these services with a single command. This approach allows you to build more complex web applications and APIs in a controlled and scalable environment.

Leave a Comment