【发布时间】: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