Search code examples
dockertraefik

docker + traefik: how to route to different containers based on host name?


Simple situation

  • an nginx container, running internally on port 80, exposed to 9000
  • an apache container, running internally on port 80, exposed to 9001

I added entry to my hosts file

  • 127.0.0.1 nginx.test apache.test

My docker-compose.yml

version: "3.9"
services:

  nginx:
    image: nginx
    container_name: nginx
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.nginx.rule=Host(`nginx.test`)"
      - "traefik.http.services.nginx.loadbalancer.server.port=80"
    ports:
      - "9000:80"

  apache:
    image: httpd
    container_name: apache
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.apache.rule=Host(`apache.test`)"
      - "traefik.http.services.apache.loadbalancer.server.port=80"
    ports:
      - "9001:80"

  proxy:
    image: "traefik:v2.5"
    container_name: "proxy"
    command:
      - "--log.level=DEBUG"
      - "--api.insecure=true"
      - "--providers.docker=true"
    depends_on:
      - nginx
      - apache
    ports:
      - "80:80"
      - "443:443"
      - "8080:8080"
    volumes:
      - ./proxy.sock:/var/run/proxy.sock:ro
    

What works is

*What doesn't works and it's what I need

please note: it's my first try with traefik. If it need some config, please help me

Inspecting traefik container, I see this in logs

2024-03-07 10:43:45 time="2024-03-07T09:43:45Z" 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 7.029037918s" providerName=docker
2024-03-07 10:43:52 time="2024-03-07T09:43:52Z" level=debug msg="FIXME: Got an status-code for which error does not match any expected type!!!: -1" module=api status_code=-1
2024-03-07 10:43:52 time="2024-03-07T09:43:52Z" 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?" providerName=docker

Solution

  • Found the problem.

    All was about

    volumes:
      - ./proxy.sock:/var/run/proxy.sock:ro
    

    I fixed pointing to docker.sock on WSL2, becase I'm running docker under wsl2 on windows 11

    Also, the destination path should point to docker.so and not proxy.sock

    This works

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