简体   繁体   English

使用traefik路由到不同的容器

[英]Using traefik to route to different containers

I have two basic flask apps that I've created containers for. 我有两个基本的烧瓶应用程序,它们是为此创建容器的。 I'm trying to use traefik to route to one container when visiting "localhost" (soon to be replaced wit an actual domain) and to another container when visiting "localhost/app2". 我正在尝试使用traefik来访问“ localhost”(即将被实际域替换)时路由到一个容器,并在访问“ localhost / app2”时路由到另一个容器。 When I do a docker-compose up and visit the trafik dashboard, I can see the urls have been created and I can successfully visit them. 当我进行docker-compose并访问trafik仪表板时,我可以看到URL已创建并且可以成功访问它们。 I can visit "localhost" and it correctly routes to my first flask app but "localhost/app2" leaves me with a 404/Not found error, likely because it's still visiting that first app. 我可以访问“ localhost”,它可以正确路由到我的第一个烧瓶应用程序,但是“ localhost / app2”使我出现404 / Not found错误,可能是因为它仍在访问该第一个应用程序。 How can I correctly route the second app? 如何正确路由第二个应用程序? Here's my docker-compose file: 这是我的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            

And the urls that get created for each respective app: 以及为每个应用创建的网址:

app1: http://172.23.0.3:8000/ app2: http://172.23.0.4:8001/ app1: http ://172.23.0.3: 8000/ app2: http ://172.23.0.4: 8001/

Thanks! 谢谢!

According to the docs, you are looking to use the path token as indicated here . 根据文档,您正在寻找使用此处指示的path令牌。

Working with docker files and labels, this should work based on this : 使用docker文件和标签,应该基于以下方法工作:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM