如何在 docker-compose 标签中设置 Traefik 2.4 服务名称

How to set Traefik 2.4 service name in docker-compose labels

有什么方法可以在 docker-compose 标签中设置 traefik.http.services 名称吗?

假设我有简单的 docker-compose.yml:

version: '3.4'

services:
  traefik:
    image: "traefik:v2.4.2"
    command:
      - --log.level=warning
      - --api.insecure=true
      - --api.dashboard=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - "80:80"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      - traefik.enable=true

      # Dashboard
      - traefik.http.routers.traefik.rule=Host(`traefik.localhost`)
      # this is interesting - traefik is naming his api service somehow
      - traefik.http.routers.traefik.service=api@internal
      - traefik.http.services.traefik.loadbalancer.server.port=8080

  whoami:
    image: "traefik/whoami"
    labels:
      - traefik.enable=true
      - traefik.http.routers.webwho.rule=Host(`who.localhost`)

效果很好,在 docker-compose up 之后我可以在 http://traefik.localhost and "whoami" at http://who.localhost

看到仪表板

问题是 'whoami' traefik 服务的名称 - 它类似于 whoami-{name_of_project} 当我想在其他标签中引用它时出现问题。

例如,我想使用新的 'foo' docker 服务作为 404.html 提供者(在这个例子中我将使用 traefik/whoami 图像,这很愚蠢,但是嘿,这只是例子 ;) )

我通过使用低优先级“捕获所有”路由器来做到这一点:

version: '3.4'

services:
  traefik:
    image: "traefik:v2.4.2"
    command:
      - --log.level=warning
      - --api.insecure=true
      - --api.dashboard=true
      - --api.debug=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      - "80:80"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      - traefik.enable=true

      # Dashboard
      - traefik.http.routers.traefik.rule=Host(`traefik.localhost`)
      # this is interesting - traefik is naming his api service somehow
      - traefik.http.routers.traefik.service=api@internal
      - traefik.http.services.traefik.loadbalancer.server.port=8080

  whoami:
    image: "traefik/whoami"
    labels:
      - traefik.enable=true
      - traefik.http.routers.webwho.rule=Host(`who.localhost`)

  foo:
    image: "traefik/whoami"
    labels:
      - traefik.enable=true
      - traefik.http.routers.error-router.rule=HostRegexp(`{host:.+}`)
      - traefik.http.routers.error-router.priority=1
      - traefik.http.routers.error-router.middlewares=error-pages-middleware
      - traefik.http.middlewares.error-pages-middleware.errors.status=400-599
      # nope!
      # actual name of this service (as seen in traefik dashboard) is something like 'foo-test'
      # and this will not work.
      # (btw, there is very clear Error Message in traefik console about service foo does not exists,
      # making debugging pleasant experience :) Thank you Traefik!)
      - traefik.http.middlewares.error-pages-middleware.errors.service=foo
      - traefik.http.middlewares.error-pages-middleware.errors.query=/{status}.html

但我不知道如何设置 traefik 服务的名称,所以我可以将其作为 traefik.http.middlewares.error-pages-middleware.errors.service=??? 标签的值引用 - 服务名称 foo-test 不是静态的('test' 是我的目录名称,其中包含 docker-compose.yml) 并且它会不时更改(尤其是与 visual studio .dcproj 一起使用时)

那么 - 有什么方法可以设置服务名称吗?


我尝试了什么:

Google & 文档。不,但也许是因为我不知道该问什么。

设置container_name: foo根本没有帮助。

有趣的是,如果我添加标签 - traefik.http.services.foo.loadbalancer.server.port=80 它会自动将 traefik 服务命名为 foo 这正是我想要的并且一切正常。但这感觉就像“B 计划”,因为我不想设置端口,我想设置服务的名称。

使用 traefic 动态配置

[http.services]
  [http.services.foo.loadBalancer]
    [[http.services.foo.loadBalancer.servers]]
      url = "http://foo:80/"

应该可以工作(我没有测试过)——但同样,我不想设置整个 url,我想设置服务名称...

看起来(至少在 v2.4 中)除了

别无他法
labels:
  - traefik.http.services.<<name_of_service>>.loadbalancer.server.port=80

据我所知,即使在文档中,也只有一个提及:

labels:
  - "traefik.http.routers.myproxy.rule=Host(`example.net`)"
  # service myservice gets automatically assigned to router myproxy
  - "traefik.http.services.myservice.loadbalancer.server.port=80"

(来源:https://doc.traefik.io/traefik/routing/providers/docker/