Traefik doesn't forward request to the service

Hi,

I would like to use Traefik as load balancer for two backend containers, which are listening on port 2500. The Traefik dashboard should be available at traefik.localhost. I also want the backend containers to automatically restart when they crashed.

When I run the docker-compose file below with docker-compose --compatibility up --build, I can access both backends directly on backend.localhost:2500 and backend.localhost:2501. However, the backends should only be available through Traefik at localhost:80. Unfortunately, when I open localhost:80, it redirects to http://localhost/dashboard/dashboard/dashboard/dashboard/....

This similar question didn't help.

Thanks for your help.

version: "3.4"

services:
  backend:
    deploy:
      replicas: 2
    labels:
      - "traefik.enable=true"
      - "traefik.http.routers.backend.rule=Host(`backend.local`)"
      - "traefik.http.routers.goserver.entrypoints=web"
      - "traefik.http.services.backend.loadbalancer.server.port=2500"
    ports:
      - "2500-2501:2500"
    build: .
    restart: always
    healthcheck:
      test: curl http://127.0.0.1:2500 -s -f -o /dev/null || exit 1
      interval: 10s
      timeout: 10s
      retries: 3
  traefik:
    image: traefik:v2.1.1
    container_name: traefik
    command:
      - --log.level=INFO
      - --entrypoints.web.address=:80
      - --api
      - --providers.docker.exposedbydefault=false
      - '--providers.docker.defaultRule=Host(`{{ index .Labels "com.docker.compose.service" }}.localhost`)'
    ports:
      - "80:80"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    labels:
      traefik.enable: true
      traefik.http.routers.traefik.service: api@internal
      traefik.http.routers.traefik.entrypoints: web

There is a lot of things here that is wrong with that config, I'm not sure I spotted them all:

  • traefik declaration does not have a deploy tag for swarm
  • traefik swarm mode of docker provider is not enabled
  • Labels are not in the deploy part of the service
  • You are trying to open localhost and you do not have a rule that would match localhost