Search code examples
docker-composetraefik

traefik v2.2 help using only docker-compose router service entrypoint


Started learning about docker, traefik for playing in home.

Aim: Put everything all together in docker-compose.yml and .env files, understand basics, comment accordingly.

Want to get dashboard from traefik.test.local/dashboard rather test.local:8080, similarly api should be accessed from traefik.test.local/api. So that don't have to think about port numbers.

added lines to /etc/hosts

127.0.0.1       test.local
127.0.0.1       traefik.test.local

docker-compose.yml

version: "3.7"

services:
  traefik:
    # The official v2 Traefik docker image
    image: traefik:v2.2
    # Lets name the container
    container_name: traefik
    command:
      # Enables the web UI
      - "--api.insecure=true" 
      # Tells Traefik to listen to docker
      - "--providers.docker"
    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
    #labels:
      #- "traefik.http.routers.router.rule=Host(`traefik.test.local/dashboard`)"
      #- "traefik.http.routers.router.rule=Host(`traefik.test.local/api`)"
    restart:
      always

Not able to understand how to connect from router to services. Also correct me if I am wrong anywhere. Thank you.

PS: OS: kde-neon


Solution

  • you can achieve this using the following definition, you need to add labels for the routers and service and not only the router

    proxy:
        image: traefik:v2.1
        command:
          - '--providers.docker=true'
          - '--entryPoints.web.address=:80'
          - '--entryPoints.metrics.address=:8082'
          - '--providers.providersThrottleDuration=2s'
          - '--providers.docker.watch=true'
          - '--providers.docker.swarmMode=true'
          - '--providers.docker.swarmModeRefreshSeconds=15s'
          - '--providers.docker.exposedbydefault=false'
          - '--providers.docker.defaultRule=Host("traefik.lvh.me")'
          - '--accessLog.bufferingSize=0'
          - '--api=true'
          - '--api.dashboard=true'
          - '--api.insecure=true'
          - '--ping.entryPoint=web'
        volumes:
          - '/var/run/docker.sock:/var/run/docker.sock:ro'
        ports:
          - '80:80'
          - '8080:8080'
        restart:
          always
        deploy:
          labels:
            - traefik.enable=true
            - traefik.docker.network=monitoring
            - traefik.http.services.traefik-dashboard.loadbalancer.server.port=8080
            - traefik.http.routers.traefik-dashboard.rule=Host(`dashboard.traefik.lvh.me`)
            - traefik.http.routers.traefik-dashboard.service=traefik-dashboard
            - traefik.http.routers.traefik-dashboard.entrypoints=web
            - traefik.http.services.traefik-api.loadbalancer.server.port=80
            - traefik.http.routers.traefik-api.rule=Host(`api.traefik.lvh.me`)
            - traefik.http.routers.traefik-api.service=traefik-api
            - traefik.http.routers.traefik-api.entrypoints=web
        logging:
          driver: json-file
          options:
            'max-size': '10m'
            'max-file': '5'
    
    

    also if you use lvh.me domain you not need to edit /etc/hosts