Docker Swarm and FastAPI with Traefik

Viewed 36

I can't seem to bring up the FastAPI with the following docker-compose.yml script in docker swarm. I am trying to launch FastAPI using Traefik as a proxy. I can go into the container and curl localhost and get the response but not on the web. All of my other sites are up.``

Here is my docker-compose.yml file

version: '3.8'

services:
  api:
    image: tiangolo/uvicorn-gunicorn-fastapi:python3.8
    networks:
      - app-network
      - traefik-public
      - database-service
      - search-service
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.constraint-label=traefik-public"
        - "traefik.docker.network=traefik-public"
        - "traefik.http.routers.${APP_NAME}-http.rule=Host(`${DOMAIN?Variable not set}`)"
        - "traefik.http.routers.${APP_NAME}-http.entrypoints=http"
        - "traefik.http.routers.${APP_NAME}-http.middlewares=https-redirect"
        - "traefik.http.routers.${APP_NAME}-https.rule=Host(`${DOMAIN?Variable not set}`)"
        - "traefik.http.routers.${APP_NAME}-https.entrypoints=https"
        - "traefik.http.routers.${APP_NAME}-https.tls=true"
        - "traefik.http.routers.${APP_NAME}-https.tls.certresolver=le"
        - "traefik.http.services.${APP_NAME}.loadbalancer.server.port=80"
      placement:
        constraints:
          - node.labels.pip.node.webservers == true
    environment:
      - "DOMAIN=${DOMAIN}"
      - "APP_NAME=${APP_NAME}"
      - "APP_FILES=${APP_FILES}"
      
networks:
  app-network:
    name: ${APP_NAME}-net
    external: true
  database-service:
    external: true
  search-service:
    external: true
  traefik-public:
    external: true

I can see the router in my traefik admin panel. But I am not getting it to forward to the api. I am thinking I don't have the "traefik.http.services.${APP_NAME}.loadbalancer.server.port=80" pointing to the right port but that is just a guess.

UPDATE: Based on the info at https://fastapi.tiangolo.com/deployment/docker/, I created my own Dockerfile.

Dockerfile

#
FROM python:3.8

#
WORKDIR /code

#
COPY ./requirements.txt /code/requirements.txt

#
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt

#
COPY ./app /code/app

#
CMD ["uvicorn", "app.main:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "80"]

Same results.

Another update - Solved.

I have updated my docker-compose.yml

version: '3.8'

services:
  api:
    image: markparrish/olis:latest
    networks:
      - app-network
      - traefik-public
      - database-service
      - search-service
    deploy:
      labels:
        - "traefik.enable=true"
        - "traefik.constraint-label=traefik-public"
        - "traefik.docker.network=traefik-public"

        - "traefik.http.routers.${APP_NAME}-http.rule=Host(`${DOMAIN?Variable not set}`)"
        # - "traefik.http.routers.${APP_NAME?Variable not set}-http.rule=PathPrefix(`/api`) || PathPrefix(`/docs`) || PathPrefix(`/redoc`)"
        - "traefik.http.routers.${APP_NAME}-http.entrypoints=http"
        - "traefik.http.routers.${APP_NAME}-http.middlewares=https-redirect"

        - "traefik.http.routers.${APP_NAME}-https.rule=Host(`${DOMAIN?Variable not set}`)"
        - "traefik.http.routers.${APP_NAME}-https.entrypoints=https"
        - "traefik.http.routers.${APP_NAME}-https.tls=true"
        - "traefik.http.routers.${APP_NAME}-https.tls.certresolver=le"
        - "traefik.http.services.${APP_NAME}.loadbalancer.server.port=80"
      placement:
        constraints:
          - node.labels.pip.node.webservers == true
networks:
  app-network:
    name: ${APP_NAME}-net
  database-service:
    external: true
  search-service:
    external: true
  traefik-public:
    external: true
0 Answers
Related