Traefik docker 容器反向代理重定向到其他容器提供的端口失败:网关超时

Traefic docker container reverse-proxy redirect fails to ports provided by other containers: Gateway timeout

设置:我有各种本机应用程序和 docker nas 设备上的应用程序。 (简单的例子)。

host
: 8080 (console)
: 81 (apache)
: <port> and more (individual nas applications)
- container:traefik
  :80
- container:nginx
  :90
- container:customcode
  :4000
- and more (individual applications)
  :<port>

(主机是192.168.1.22).

**所有容器和应用程序都可以工作并且可以通过“http://192.168.1.22:<port>

访问

我试图使用具有简单名称的 traefik 来管理端口。 即

traefik 设置能够重定向到主机本身的所有端口,但none docker 公开的端口。这也适用于不同主机上的站点。对于容器暴露的端口,我得到一个 'Gateway timeout' 错误

(仅日志文件条目:"'504 Gateway Timeout' caused by: dial tcp 192.168.1.22:90: i/o timeout")。

我不能在容器上使用标签,因为它们不(有些不能)共享网络。我只想让 traefik 路由到 IP:Port 而不必担心端口是否由容器提供。

traefik.toml

loglevel = "ERROR"

[Log]
  filePath = "/etc/traefik/traefik.log"
  level = "DEBUG"  

[entryPoints]
  [entryPoints.http]
  address = ":80"
  [entryPoints.https]
  address = ":443"

[api]
  dashboard = true

[providers.docker]
  watch = false
  exposedByDefault = false
  endpoint = "unix:///var/run/docker.sock"  
  
[providers.file]
  watch = true
  filename = "/etc/traefik/services.toml"

services.toml

[http]
  [http.services]
    [http.services.nas]
      [http.services.nas.loadBalancer]
        [[http.services.nas.loadBalancer.servers]]
          url = "http://192.168.1.22:8080/"
    [http.services.test90]
      [http.services.test90.loadBalancer]
        [[http.services.test90.loadBalancer.servers]]
          url = "http://192.168.1.22:90/" #this does not work#
    [http.services.test81]
      [http.services.test81.loadBalancer]
        [[http.services.test81.loadBalancer.servers]]
          url = "http://192.168.1.22:81/"

docker撰写:

version: "3.5"
services:
  traefik:
    image: "traefik:2.4"
    container_name: "traefik"
    restart: always
    environment:
      - PUID=<id>
      - PGID=<id>
    ports:
      - "443:443"
      - "80:80"
    volumes:
      - "/shr/traefik/:/etc/traefik/"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.traefik.rule=Host(`traefik`)"
      - "traefik.http.routers.traefik.service=api@internal"
      - "traefik.http.routers.traefik.entrypoints=http,https"
      - "traefik.http.routers.traefik.middlewares=auth"
      - "traefik.http.middlewares.auth.basicauth.users=admin:<pass>"

      - "traefik.http.routers.nas.entrypoints=http"
      - "traefik.http.routers.nas.rule=Host(`nas`)"
      - "traefik.http.routers.nas.service=nas@file"

      - "traefik.http.routers.test81.entrypoints=http"
      - "traefik.http.routers.test81.rule=Host(`apache`)"
      - "traefik.http.routers.test81.service=test81@file"

      - "traefik.http.routers.test90.entrypoints=http"
      - "traefik.http.routers.test90.rule=Host(`nginx`)"
      - "traefik.http.routers.test90.service=test90@file"
    networks:
      - proxy

  whoami:
    image: "traefik/whoami"
    container_name: "whoami"
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami`)"
      - "traefik.http.routers.whoami.entrypoints=http"

    networks:
      - proxy

networks:
  proxy:
    external:
      name: proxy

添加 network_mode: "host" 到 docker-compose 并删除自定义网络似乎解决了这个问题。

您可能需要在导致问题的应用程序中设置正确的 traefik 网络:

在docker-compose.yml:

labels:
[...]
 - "traefik.enable=true"
 - "traefik.docker.network=foobar"
[...]

其中“foobar”是 traefik 所在的 docker 网络。理想情况下,外部 docker 网络。