首页 > 解决方案 > Traefic docker 容器反向代理重定向到其他容器提供的端口失败:网关超时

问题描述

设置:我在一个 nas 设备上有各种原生应用程序和 docker 应用程序。(简单的例子)。

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 来管理端口。IE

traefik 设置能够重定向到主机本身的所有端口,但不会重定向到 docker 暴露的任何端口。这也适用于不同主机上的站点。对于容器公开的端口,我收到“网关超时”错误

(仅日志文件条目:)"'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"

服务.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/"

码头工人撰写:

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

标签: dockertraefik

解决方案


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


推荐阅读