How to specify server port in dynamic configuration?

We are able to define the following label for the service:

traefik.http.services.myservice.loadbalancer.server.port=9000

What is the equivalent of the above line in file provider? I suppose it should be something like this, but I didn't find any documentation.

http:
  services:
    myservice:
      loadbalancer:
        server:
          port: 9000

Hello,

In the file provider, a server must be describe by an URL:

http:
  services:
    my-service:
      loadBalancer:
        servers:
          - url: "http://foobar.com:9080"

https://docs.traefik.io/v2.0/routing/services/#servers

The label traefik.http.services.myservice.loadbalancer.server.port exists because for a Docker container a user cannot known the IP.

Let's say I have the following dynamic configuration saved in file in config folder:

http:
  services:
    traefik:
      loadBalancer:
        passHostHeader: true
    whoami:
      loadBalancer:
        passHostHeader: true
        sticky:
          cookie:
            name: StickyCookie
  routers:
    traefik:
      rule: "Host(`example.com`)"
      entryPoints:
        - "traefik"
      service: traefik
    whoami:
      rule: "Host(`example.me`)"
      entryPoints:
        - "web"
      service: whoami

And the following docker-compose file:

version: "3.7"

services:
  traefik:
    image: "traefik:v2.0"
    networks:
      - traefik-net
    ports:
      - "9000:9000"
      - "5000:5000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./config/:/etc/traefik"
    deploy:
      replicas: 1
      labels:
        - "traefik.http.services.traefik.loadbalancer.server.port=8080"

networks:
  traefik-net:
    external:
      name: traefik-net

When I execute docker stack deploy, it creates 3 services:

  1. traefik (provider file)
  2. traefik (provider Docker)
  3. whoami (provider file)

This is not what I want. I just want to specify port for traefik service which is defined in file, I don't want to create another service. How can I reference existing traefik service from labels section so that it wouldn't create new one?

Why are you using file you define routers on containers?
why not just use labels?


Only labels (I recommend):

version: "3.7"

services:
  traefik:
    image: "traefik:v2.0"
    networks:
      - traefik-net
    ports:
      - "9000:9000"
      - "5000:5000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./config/:/etc/traefik"
    deploy:
      replicas: 1
      labels:
        - "traefik.http.routers.traefik.rule=Host(`example.com`)"
        - "traefik.http.routers.traefik.entrypoints=traefik"
        - "traefik.http.routers.traefik.service=api@internal"
        - "traefik.http.services.traefik.loadbalancer.server.port=8080" # noop

  whoami:
    image: containous/whoami:v1.3.0
    networks:
      - traefik-net
    deploy:
      labels:
        - "traefik.http.routers.whoami.rule: Host(`example.me`)"
        - "traefik.http.routers.myrouter.entrypoints=web"
        - "traefik.http.services.whoami.loadbalancer.sticky.cookie.name=StickyCookie"

networks:
  traefik-net:
    external:
      name: traefik-net

Mixed labels and file (I don't recommend in your case):

version: "3.7"

services:
  traefik:
    image: "traefik:v2.0"
    networks:
      - traefik-net
    ports:
      - "9000:9000"
      - "5000:5000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - "./config/:/etc/traefik"
    deploy:
      replicas: 1
      labels:
        - "traefik.enable=false"

  whoami:
    image: containous/whoami:v1.3.0
    networks:
      - traefik-net
    deploy:
      labels:
        - "traefik.http.services.whoami.loadbalancer.sticky.cookie.name=StickyCookie"

networks:
  traefik-net:
    external:
      name: traefik-net
http:
  routers:
    traefik:
      rule: "Host(`example.com`)"
      entryPoints:
        - "traefik"
      service: api@internal
    whoami:
      rule: "Host(`example.me`)"
      entryPoints:
        - "web"
      service: whoami@docker

It cannot be done by using only file.