简体   繁体   中英

Cannot set Traefik via “labels” inside docker-compose.yml

Traefik simply ignores "labels" configuration.

Following Traefik's main documentation page , we can simply do:

#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"

Then, running docker-compose should be enough (without traefik.toml file):

docker-compose up -d

Results in the expected:

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

But unfortunately, Traefik's dashboard displays nothing:

No providers found

What have I tried to do:

  • To use also another labels notation:
 labels: traefik.backend: "whoami" traefik.frontend.rule: "Host:whoami.docker.localhost" 
  • To follow this guide .
  • To remove "version: '3'", and also changing it to "version: '3.3'".
  • To run $env:DOCKER_HOST="npipe:////./pipe/docker_engine" or $env:DOCKER_HOST="tcp://localhost:2375" on Powershell.
  • To set npipe instead of unix socket:
 volumes: - type: npipe source: ./pipe target: /pipe/docker_engine 

Nothing works.

Right now, the only way I can see something in the dashboard, is by adding this line to Traefik volumes: "- ./traefik.toml:/traefik.toml", while creating "traefik.toml" file with [file] configurations. I don't want to have this file. I want to have the control inside the "lables" inside docker-compose.yml.

It'll also be nice to know when should I use the traefik.toml file, as opposed to setting lables inside docker-compose.yml. I did not see any information on that.

Edit: docker logs of traefik shows UNIX socket is in use:

time="2018-07-23T10:55:38Z" level=error msg="Failed to retrieve information of the docker client and server host: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"

time="2018-07-23T10:55:38Z" level=error msg="Provider connection error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?, retrying in 13.276739006s"

Docker-compose should use npipe protocol on Windows by default , but it doesn't. Trying to set the Windows' pipe explicitly, to take place instead of the UNIX socket ( 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

But still the logs shows:

time="2018-07-23T10:57:18Z" level=error msg="Provider connection error Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?, retrying in 4.166259863s"

time="2018-07-23T10:57:23Z" level=error msg="Failed to retrieve information of the docker client and server host: Cannot connect to the Docker daemon at unix:///var/run/docker.sock. Is the docker daemon running?"

It's possible to use:

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

Only with this workaround in Powershell:

$Env:COMPOSE_CONVERT_WINDOWS_PATHS=1

The reason is this opened bug: https://github.com/docker/for-win/issues/1829 which makes it impossible to mount docker.sock, because it is "not a valid Windows path" (error).

The docker.sock file is located at /var/run/docker.sock on a linux environment, not the current directory. Therefore the volume mount is incorrect in your example for a linux environment. That compose file would look like:

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"

Since you are running the command on windows, the docker api is accessed via an npipe or tcp connection . The solution to this appears to be (this may depend on your version of 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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM