How can I properly setup basic fastapi with traefik reverse proxy?

Assume my current public IP is, I have a domain called which I configured using cloudflare and I created multiple DNS entry pointing to my public ip.


    1) -
    2) -
    3) -

Now, Here's my example project structure,

├── myapp
│   ├── app
│   │   └──
│   ├── docker-compose.yml
│   └── Dockerfile
├── myapp1
│   ├── app
│   │   └──
│   ├── docker-compose.yml
│   └── Dockerfile
└── traefik
    ├── acme.json
    ├── docker-compose.yml
    ├── traefik_dynamic.toml
    └── traefik.toml

Here I have two fastAPIs (i.e., myapp, myapp1)

Here's the example code I have in in both myapp and myapp1, Its exactly same but return staement is different that's all

from fastapi import FastAPI
app = FastAPI()
def read_main():
    return {"message": "Hello world for my project myapp"}

Here's my Dockerfile for myapp and myapp1, here too both are exactly same but the only difference is I deploy myapp on 7777 and myapp1 on 7778 in different containers

FROM ubuntu:latest

ARG DEBIAN_FRONTEND=noninteractive
RUN apt update && apt upgrade -y
RUN apt install -y -q build-essential python3-pip python3-dev

# python dependencies
RUN pip3 install -U pip setuptools wheel
RUN pip3 install gunicorn fastapi uvloop httptools "uvicorn[standard]"

# copy required files
RUN bash -c 'mkdir -p /app'
COPY ./app /app

ENTRYPOINT /usr/local/bin/gunicorn \
    -b \ # this line I use for myapp dockerfile
    -b \ # this line I change for myapp1 dockerfile
    -w 1 \
    -k uvicorn.workers.UvicornWorker app.main:app \
    --chdir /app

Here's my docker-compose.yml file for myapp and myapp1, here also I have exactly same but only difference is I change the port,

  myapp:  # I use this line for myapp docker-compose file
  myapp1: # I use this line for myapp1 docker-compose file
    build: .
    restart: always
      - "traefik.enable=true"
      - ""

      - "traefik.backend=myapp" # I use this line for myapp docker-compose file
      - "traefik.backend=myapp1" # I use this line for myapp1 docker-compose file

      - "" # I use this for myapp compose file
      - "" # I use this for myapp1 compose file

      - "traefik.port=7777" # I use this line for myapp docker-compose file
      - "traefik.port=7778" # I use this line for myapp1 docker-compose file
      - traefik_public

    external: true

Now coming to traefik folder,

  1. acme.json # I created it using nano acme.json command with nothing in it,
    but did chmod 600 acme.json for proper permissions.

  2. traefik_dynamic.toml

      entryPoints = ["web"]
      middlewares = ["my-basic-auth"]
      service = "api@internal"
      rule = "Host(``)"
        certResolver = "myresolver"

  users = [
  1. traefik.toml
    address = ":80"
          to = "websecure"
          scheme = "https"

    address = ":443"

  dashboard = true

  email = ""
  storage= "acme.json"
    entryPoint = "web"

    watch = true
    network = "web"
    filename = "traefik_dynamic.toml"
  1. docker-compose.yml
    image: traefik:latest
      - 80:80
      - 443:443
      - 8080:8080
      - /var/run/docker.sock:/var/run/docker.sock
      - ./traefik.toml:/traefik.toml
      - ./acme.json:/acme.json
      - ./traefik_dynamic.toml:/traefik_dynamic.toml
      - web


These are the details about my files, what I am trying to achieve here is,

I want to setup traefik and traefik dashboard with basic authentication, and I deploy two of my fastapi services,

  • myapp 7777, I need to access this app via
  • myapp1 7778, I need to access this app via
  • traefik dashboard, I need to access this via

All of these should be https and also has certification autorenew enabled.

I got all these from online articles for latest version of traefik. But the problem is this is not working. I used docker-compose to build and deploy the traefik and I open the api dashboard. It is asking for password and user (basic auth I setup) I entered my user details I setup in traefik_dynamic.toml but its not working.

First of all is what am I doing possible or not? If its possible can someone help on understand where I went wrong. Please help me correcting mistakes in my configuration. I am really interested to learn more about this but there's no proper online articles which helps.

can anyone help out?