How to use basic auth middleware?

I am trying to set up the basic auth with a docker provider.
I've added auth label from documentation as is to the service, but there is no auth prompt in the browser.

docker-compose.yml

version: '3'

services:
  reverse-proxy:
    # The official v2.0 Traefik docker image
    image: traefik:v2.2
    # Enables the web UI and tells Traefik to listen to docker
    command:
      - --api.insecure=true
      - --providers.docker
      - --accesslog=true
    ports:
      # The HTTP port
      - "80:80"
      # The Web UI (enabled by --api.insecure=true)
      - "8080:8080"
    volumes:
      # So that Traefik can listen to the Docker events
      - /var/run/docker.sock:/var/run/docker.sock
      - .:/app

  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=PathPrefix(`/`)
      - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"  

The labels were properly added to the container:

            "Labels": {
                "com.docker.compose.config-hash": "446f86d3954c8693a3e1c245593062cf0de299568fb5e1af27e1eb910b4e13fe",
                "com.docker.compose.container-number": "1",
                "com.docker.compose.oneoff": "False",
                "com.docker.compose.project": "traefik",
                "com.docker.compose.project.config_files": "docker-compose.yml",
                "com.docker.compose.project.working_dir": "/home/maks/Learning/traefik",
                "com.docker.compose.service": "whoami",
                "com.docker.compose.version": "1.25.5",
                "traefik.http.middlewares.test-auth.basicauth.users": "test:$apr1$H6uskkkW$IgXLP6ewTrSuBkTrqE8wj/,test2:$apr1$d9hr9HBB$4HxwgUir3HP4EsggP/QNo0",
                "traefik.http.routers.whoami.rule": "PathPrefix(`/`)"
            }

I have also tried with different parameters and in different projects. Result the same - nothing happens.
What I am doing wrong? :joy:

1 Like

Ok, my problem was that line

- "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0"

only creates a middleware, but it should be explicitly applied to the router by:

- traefik.http.routers.whoami.middlewares=test-auth

So the final configuration of whoami service:

  whoami:
    # A container that exposes an API to show its IP address
    image: containous/whoami
    labels:
      - traefik.http.routers.whoami.rule=PathPrefix(`/`)
      - "traefik.http.middlewares.test-auth.basicauth.users=test:$$apr1$$H6uskkkW$$IgXLP6ewTrSuBkTrqE8wj/,test2:$$apr1$$d9hr9HBB$$4HxwgUir3HP4EsggP/QNo0" 
     - traefik.http.routers.whoami.middlewares=test-auth

that's what I missed from the middleware overview documentation page :rofl: