TechWisdom
TechWisdom

Reputation: 4305

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:

labels:
  traefik.backend: "whoami"
  traefik.frontend.rule: "Host:whoami.docker.localhost"
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?"

Upvotes: 2

Views: 4905

Answers (2)

TechWisdom
TechWisdom

Reputation: 4305

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).

Upvotes: 7

BMitch
BMitch

Reputation: 264406

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"

Upvotes: 0

Related Questions