Traefik:使用docker的配置无法正常运行

时间:2020-05-27 07:33:07

标签: docker traefik

我正在尝试在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"

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

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

1 个答案:

答案 0 :(得分:2)

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

您可以通过以下方式修改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)。

仅此而已。

我希望这会有所帮助

相关问题