defaultRule to get the service name without the stack

I want an automatic rule that goes something like

stack == intranet

service is whoami in the docker-compose.yml which yields intranet_whoami as the service name.

I sort of want it to yield PathPrefix('/whoami') without being explicitly set.

Any suggestions on how to set up the rule?

The rule I think would look something like this

{{ .ServiceName | replace .StackName \"\" }}

The data is in the labels

       "Labels": {
                "com.docker.stack.namespace": "intranet",
                "com.docker.swarm.node.id": "qosf0b21u821yqgxr4tvmabqi",
                "com.docker.swarm.service.id": "chgy8u0kf9n1qr2fbwgjl5dyk",
                "com.docker.swarm.service.name": "intranet_whoami",
                "com.docker.swarm.task": "",
                "com.docker.swarm.task.id": "6dgsqlz7zsdg1yovbgtyuvna6",
                "com.docker.swarm.task.name": "intranet_whoami.1.6dgsqlz7zsdg1yovbgtyuvna6"
            }
{{ .ServiceName | replace (index .Labels \"com.docker.stack.namespace\") \"\" }}
1 Like
labels:
  - '--providers.docker.defaultrule=Host(`{{ index .Labels "com.docker.swarm.service.name" | trimPrefix (index .Labels "com.docker.stack.namespace") | trimPrefix "_" }}.docker.localhost`)'
labels:
  - '--providers.docker.defaultrule=PathPrefix(`/{{ index .Labels "com.docker.swarm.service.name" | trimPrefix (index .Labels "com.docker.stack.namespace") | trimPrefix "_" }}`)'

Note:

  • I use simple quote ' to "wrap" the label
  • I use double qoutes " for the strings

Doc:

1 Like

OK I like yours better than what I eventually ended up with

- "--providers.docker.defaultRule={{ substr (int ( add (len (index .Labels `com.docker.stack.namespace`)) 1)) 9999 .Name }}"

Minor correction though, I wanted the path Prefix, so I am using

- '--providers.docker.defaultrule=PathPrefix(`/{{ .Name | trimPrefix (index .Labels "com.docker.stack.namespace") | trimPrefix "_" }}`)'

Note I used .Name which is the service name according to the documentation.

So my final setup is

  whoami:
    image: containous/whoami
    deploy:
      resources:
        limits:
          cpus: '0.5'
          memory: 32M
      labels:
        - intranet=true
        - traefik.enable=true
        # - traefik.http.routers.whoami.rule=Path(`/whoami`)
        # - traefik.http.routers.whoami.entryPoints=http
        # - traefik.http.routers.whoami.middlewares=default
        # - traefik.http.routers.whoami.service=ping
        - traefik.http.services.whoami.loadbalancer.server.port=80
    networks:
      - intranet

Something seems to have changed in 2.0.1 this seems to put - instead of _ now. My workaround for now is to add trimPrefix "_" | trimPrefix "-" so it will work with both 2.0.0 and 2.0.1

1 Like

Unless you want to get crazy trying to understand why the suggest approach doesn't work, use the normalize function to get rid of underscore to hypen conversions applied by docker:

 - --providers.docker.defaultRule=Host(`example.com`) && PathPrefix(`/{{ ( .Name | trimPrefix (normalize (index .Labels "com.docker.stack.namespace") ) | trimPrefix "-" )  }}`)