Traefik:docker 的配置不起作用

Traefik: Configuration with docker not working

我正在尝试在 Docker 中使用 Traefik 反向代理设置示例应用程序。

我正在为这个项目使用 Traefik v2.2,它与 Traefik.v1.0 有显着差异。

这是我的 docker-compose.yml 文件:

version: '3'

services:
  traefik:
    # The official v2 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --api.insecure=true
      - --providers.docker=true
      - --providers.docker.exposedbydefault=false
      - --entrypoints.web.address=:80
    ports:
      # The HTTP port
      - "89:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8089:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.whoami.rule=Host(`whoami.localhost`)"
      - "traefik.http.routers.whoami.entrypoints=web"

当我在我的网络浏览器上转到 localhost:8089 时,我可以访问 Traefik 的仪表板,但是当我在我的网络浏览器上输入 whoami.localhost 地址时,我无法访问 whoami 应用程序.我只是想知道在访问它之前是否需要更改任何内容,或者我是否需要将主机从 whoami.localhost 更改为 localhost:3000 因为这是我想要访问应用程序的端口.

我们将不胜感激任何形式的帮助。谢谢。

我发现的一个问题是您将 traefik 容器的容器端口 80 暴露给主机端口 89。如果您在 Web 浏览器中输入 whoami.localhost,您的浏览器将在该地址的主机端口 80 上搜索应用程序(因为 localhost 本机映射到端口 80 ),但它不会在那里找到任何东西,因为它只能在端口 89 处找到。据我了解,您应该能够使用命令 curl -H Host:whoami.localhost http://127.0.0.1:89 通过命令行访问该应用程序。不幸的是,我不确定您的浏览器如何分别通过您的 DNS 处理 URL whoami.localhost:89

您可以这样修改 docker-compose.yml 文件:

version: "3"

services:
  traefik:
    # The official v2 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --api.insecure=true
      - --providers.docker=true
    ports:
      # The HTTP port
      - "89:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8089:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=Host(`whoami.localhost`)

然后您可以在命令终端上输入以下内容来访问该应用程序:

curl -H Host:whoami.localhost http://127.0.0.1:89

注意whoami.localhost可以是whoami.docker.localhostapp.localhost或任何你想要的。这里的事情是你应该 localhost 附加到最后,除非你要添加一个完全合格的域名 (FQDN)。

就这些了。

希望对您有所帮助