使用traefik路由到不同的容器

Using traefik to route to different containers

我有两个基本的 Flask 应用程序,我为其创建了容器。我正在尝试使用 traefik 在访问 "localhost" 时路由到一个容器(很快将被实际域替换)并在访问 "localhost/app2" 时路由到另一个容器。当我执行 docker-compose 并访问 trafik 仪表板时,我可以看到 url 已创建并且我可以成功访问它们。我可以访问 "localhost" 并且它正确地路由到我的第一个 flask 应用程序但是 "localhost/app2" 给我留下了 404/Not found 错误,可能是因为它仍在访问第一个应用程序。如何正确路由第二个应用程序?这是我的 docker-compose 文件:

version: '3'

services:
  app1:
    build: .
    command: /usr/bin/python3 fapp1.py
    networks:
      - test_network
      - internal
    ports:
      - "8000:8000"      
    labels:
    - "traefik.frontend.rule=Host:localhost"

  app2:
    build: .
    command: /usr/bin/python3 fapp2.py
    networks:
      - test_network
      - internal
    ports:
      - "8001:8001"      
    labels:
    - "traefik.frontend.rule=Host:localhost/app2"

  reverse-proxy:
    image: traefik # The official Traefik docker image
    command: --api --docker # Enables the web UI and tells Traefik to listen to docker
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker eventsdoc
    networks:
      - test_network
      - internal

networks:
  test_network:
    external: true
  internal:
    external: false            

以及为每个相应应用创建的网址:

app1:http://172.23.0.3:8000/ 应用程序 2:http://172.23.0.4:8001/

谢谢!

根据文档,您希望使用 path 标记 here

使用 docker 个文件和标签,这应该基于 this

- traefik.frontend.rule=Host:localhost;PathPrefixStrip:/app2