【问题标题】:Cannot set Traefik via "labels" inside docker-compose.yml无法通过 docker-compose.yml 中的“标签”设置 Traefik
【发布时间】:2018-07-22 15:04:49
【问题描述】:

Traefik 只是忽略“标签”配置。

按照Traefik's main documentation page,我们可以这样做:

#docker-compose.yml

version: '3'

services:

  traefik:
    image: traefik # The official Traefik docker image
    command: --api --docker # Enables the web UI and tells Træfik to listen to docker
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    volumes:
      - ./docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events

  whoami:
      image: emilevauge/whoami # A container that exposes an API to show its IP address
      labels:
        - "traefik.frontend.rule=Host:whoami.docker.localhost"

那么,运行 docker-compose 就足够了(没有 traefik.toml 文件):

docker-compose up -d

预期结果:

Starting test_traefik_1 ... done
Starting test_whoami_1  ... done

但不幸的是,Traefik 的仪表板什么也没显示:

未找到提供者

我尝试过做什么:

  • 也可以使用另一个labels 表示法:
labels:
  traefik.backend: "whoami"
  traefik.frontend.rule: "Host:whoami.docker.localhost"
  • 关注this guide
  • 删除“版本:'3'”,并将其更改为“版本:'3.3'”。
  • 在 Powershell 上运行 $env:DOCKER_HOST="npipe:////./pipe/docker_engine"$env:DOCKER_HOST="tcp://localhost:2375"
  • 设置 npipe 而不是 unix 套接字:
volumes:
  - type: npipe
    source: ./pipe
    target: /pipe/docker_engine

没有任何作用。

现在,我可以在仪表板中看到某些内容的唯一方法是将此行添加到 Traefik 卷:“- ./traefik.toml:/traefik.toml”,同时使用 [ 创建“traefik.toml”文件文件]配置。我不想拥有这个文件。我想在 docker-compose.yml 中的“标签”内拥有控件。

知道什么时候应该使用 traefik.toml 文件也很好,而不是在 docker-compose.yml 中设置标签。我没有看到任何相关信息。

编辑: traefik 的 docker 日志显示 UNIX 套接字正在使用中:

time="2018-07-23T10:55:38Z" level=error msg="检索失败 docker客户端和服务器主机信息:无法连接 unix:///var/run/docker.sock 上的 Docker 守护进程。是 docker 守护进程 跑了吗?”

time="2018-07-23T10:55:38Z" level=error msg="Provider 连接错误 无法连接到 Docker 守护进程 unix:///var/run/docker.sock。 docker 守护进程是否正在运行?,重试 在 13.276739006s"

Docker-compose should use npipe protocol on Windows by default,但它没有。尝试显式设置 Windows 的管道,而不是 UNIX 套接字 (using npipe of the long syntax):

#docker-compose.yml

version: '3.2'

services:

  traefik:
    image: traefik
    command: --api --docker
    ports:
      - "80:80"
      - "8080:8080"
    volumes:
      - type: npipe                # here we are
        source: ./pipe
        target: /pipe/docker_engine

但日志仍然显示:

time="2018-07-23T10:57:18Z" level=error msg="提供者连接错误 无法连接到 unix:///var/run/docker.sock 上的 Docker 守护程序。是 docker daemon 正在运行?,在 4.166259863s 重试”

time="2018-07-23T10:57:23Z" level=error msg="检索失败 docker客户端和服务器主机信息:无法连接 unix:///var/run/docker.sock 上的 Docker 守护进程。是 docker 守护进程 跑了吗?”

【问题讨论】:

  • 可以发一下traefik的docker日志吗?
  • @OlivierCazade 是的,我已经添加了日志。谢谢!
  • 我认为这与docker.sock的安装方式有关。只是一个猜测,但如果你使用 docker run(没有 docker-compse)运行 whoami,你将遇到同样的错误。我不知道 windows 上的 docker 是如何工作的,所以我帮不上忙。
  • 我希望 docker 在 Windows 10 上使用命名管道(npipe)而不是 docker.sock(unix 套接字)(如文档所述)。似乎是一个错误。谢谢@OlivierCazade

标签: docker docker-compose reverse-proxy traefik


【解决方案1】:

可以使用:

volumes:
   - /var/run/docker.sock:/var/run/docker.sock

仅在 Powershell 中使用此解决方法:

$Env:COMPOSE_CONVERT_WINDOWS_PATHS=1

原因是这个打开的bug:https://github.com/docker/for-win/issues/1829 这使得无法挂载 docker.sock,因为它“不是有效的 Windows 路径”(错误)。

【讨论】:

    【解决方案2】:

    docker.sock 文件在 linux 环境下位于 /var/run/docker.sock,而不是当前目录。因此,在您的 linux 环境示例中,卷挂载不正确。该撰写文件看起来像:

    version: '3'
    
    services:
    
      traefik:
        image: traefik # The official Traefik docker image
        command: --api --docker
        ports:
          - "80:80"     # The HTTP port
          - "8080:8080" # The Web UI (enabled by --api)
        volumes:
          - /var/run/docker.sock:/var/run/docker.sock
    
      whoami:
          image: emilevauge/whoami 
          labels:
            - "traefik.frontend.rule=Host:whoami.docker.localhost"
    

    由于您是在 Windows 上运行命令,因此 docker api 被访问 via an npipe or tcp connection。解决方案似乎是(这可能取决于您的 docker for windows 版本):

    version: '3'
    
    services:
    
      traefik:
        image: traefik # The official Traefik docker image
        command: --api --docker --docker.endpoint=npipe:////./pipe/docker_engine
        ports:
          - "80:80"     # The HTTP port
          - "8080:8080" # The Web UI (enabled by --api)
        volumes:
          - //./pipe/docker_engine://./pipe/docker_engine
    
      whoami:
          image: emilevauge/whoami 
          labels:
            - "traefik.frontend.rule=Host:whoami.docker.localhost"
    

    【讨论】:

    • 非常感谢您的回答。我使用了您的解决方案(没有卷,对吗?),并得到了这个错误日志:Failed to retrieve information of the docker client and server host: error during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.21/version: dial tcp: lookup //./pipe/docker_engine: no such host。似乎 //./pipe/docker_engine 路径以某种方式丢失。但至少我不再收到任何 unix 套接字错误。你会建议我尝试什么?
    • 如果我在使用你们的卷,我在创建容器时会遇到错误:ERROR: for test_traefik_1 Cannot create container for service traefik: b'Mount denied:\nThe source path "\\\\\\\\.\\\\pipe\\\\docker_engine://./pipe/docker_engine"\nis not a valid Windows path'
    • @TechWisdom 上面列出的解决方案列出了一个卷。如果它对您不存在,那么它可能已被移动或者是依赖于版本的路径。您是否尝试过将完整路径 /var/run/docker.sock 作为套接字的源和目标。你的相对路径在 windows 或 linux 上是行不通的。
    • 我刚刚尝试了完整路径:- //./pipe/docker_engine://./pipe/docker_engine 并得到:ERROR: for traefik Cannot create container for service traefik: b'Mount denied:\nThe source path "\\\\\\\\.\\\\pipe\\\\docker_engine://./pipe/docker_engine"\nis not a valid Windows path'- /var/run/docker.sock:/var/run/docker.sock 也显示:ERROR: for traefik Cannot create container for service traefik: b'Mount denied:\nThe source path "\\\\var\\\\run\\\\docker.sock:/var/run/docker.sock"\nis not a valid Windows path'。我的主机是 Windows 10,我确实使用了您所描述的完整路径。怎么了?
    猜你喜欢
    • 2019-03-17
    • 2023-01-07
    • 2022-08-08
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多