【问题标题】:Docker Swarm and FastAPI with TraefikDocker Swarm 和 FastAPI 与 Traefik
【发布时间】:2022-10-20 00:31:40
【问题描述】:

我似乎无法在 docker swarm 中使用以下 docker-compose.yml 脚本启动 FastAPI。我正在尝试使用 Traefik 作为代理启动 FastAPI。我可以进入容器并 curl localhost 并获得响应,但不能在网络上。我所有其他网站都已启动。``

这是我的 docker-compose.yml 文件

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

我可以在我的 traefik 管理面板中看到路由器。但我没有让它转发到 api。我在想我没有指向正确端口的“traefik.http.services.${APP_NAME}.loadbalancer.server.port=80”,但这只是一个猜测。

更新:根据https://fastapi.tiangolo.com/deployment/docker/ 的信息,我创建了自己的 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"]

结果相同。

另一个更新 - 已解决。

我已经更新了我的 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

【问题讨论】:

    标签: python docker fastapi docker-swarm traefik


    【解决方案1】:

    我也努力尝试让 FastAPI 中的代码运行并放弃。 相反,我发现这个网站显示了一个运行的示例。也许它可以帮助您找到问题: https://blog.creekorful.org/2019/10/how-to-install-traefik-2-docker-swarm/

    【讨论】:

      猜你喜欢
      • 2022-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多