Search code examples
traefik

How can i use traefik2.0 in docker swarm, i don't know which labels ishould use


I want use traefik2.0 publish port 80 and 7000, the port 7000 is for frp(TCP). Now i am testing locally with 2.0 doc, i am using example on quick start but not running.

This is my docker compose file.

version: '3'

services:
  reverse-proxy:
    image: traefik:v2.0 # The official v2.0 Traefik docker image
    command:
    - "--api"
    - "--entrypoints='Name:http Address::80'"
    - "--providers.docker" # Enables the web UI and tells Traefik to listen to docker
    - "--providers.docker.swarmmode=true"
    - "--providers.docker.watch=true"
    ports:
      - "80:80"     # The HTTP port
      - "8080:8080" # The Web UI (enabled by --api)
    networks:
      - traefik-net
    deploy:
      replicas: 1
      placement:
        constraints:
          - node.role == manager
      labels:
        - traefik.enable=false
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # So that Traefik can listen to the Docker events

  whoami:
    image: containous/whoami # A container that exposes an API to show its IP address
    networks:
      - traefik-net
    deploy:
      labels:
        - "traefik.http.routers.whoami.tls=true"
        - "traefik.http.routers.whoami.entrypoints=https"
        - "traefik.http.routers.whoami.rule=Host(`whoami.domain.com`)"
        - "traefik.http.middlewares.whoami.redirectscheme.scheme=https"

networks:
  traefik-net:
    external: true

i expect which labels used on traefik2.0 can work


Solution

  • You're almost there!

    Replace

    - "--entrypoints='Name:http Address::80'"

    with

    - "--entryPoints.web.address=:80"
    - "--entrypoints.websecure.address=:443"
    

    Enable the dashboard in a non-production environment. You'll also need to replace

    - "--api" with - "--api.insecure=true"

    One of the labels of whoami has a mistake. There is no https entrypoint, it's now called websecure. So change

    - "traefik.http.routers.whoami.entrypoints=https"

    with

    - "traefik.http.routers.whoami.entrypoints=websecure"

    And finally expose the internal port that the whoami app is running on.

    By adding this to the label of whoami

    - traefik.http.services.whoami-service.loadbalancer.server.port=80

    You should be able to verify it using the traefik dashboard on localhost:8080