Template for defaultRule when using portainer and stacks

Hello, I'm using portainer and I started using stacks instead of standalone container purely because changing the labels on a running container is a pain, the stack will enable me to do this in an easyer way.

My issue right now is that the defaultRule for the docker provider is now appending the stack name to the name...

My current rule in traefik.toml is

   defaultRule = "Host(`{{ normalize .Name }}.mydomain.com`)"

And for a container named container in a stack named stack I need to type https://container-stack.mydomain.com/ instead of simply https://container.mydomain.com/

Is there some other variable instead of Name that I could use ? I was not able to find the available variables in the documentation, maybe I'm just too bad at reading, sorry...

Thanks in advance !

I decided to add a label to be used in the default rule but that's yet another label I need not to forget... Any workaround to use the container name would be appreciated

I'm still looking for help on this if anyone has any idea...

I think you are mixing concepts between standalone and docker swarm.
If you are using docker swarm have a look static configuration for docker provider

About labels --> Attach labels to services (not to containers) in Swarm mode
Have a look routing for docker provider in swarm in official documentation

Thanks @diegodevops for your pointers.

Looking at it I (re-) discovered some options that will indeed be very useful like useBindPortIP or network that will help in other issues I encountered and did not post here yet, will probably not need to :wink:

Maybe I can ask more precisely for help since in fact, I'm still having some troubles... I'm probably looking for help in creating a sprig template that will remove eveything befor the first hyphen in the normalized .Name of a container, so "foobar-foo" would turn into "foo".

Another option would be to have more information on the variables available to be used in this sprig template, since in the doc I only find .Name and labels, probably other properties of the container/service are accessible to this template engine that would simplify my building of the default-rule.

In fact, I'm not using a swarm, I do have a single docker engine running, but I deploy every container I need using the portianer GUI and pass it a docker-compose file in something that portainer calls a "stack" that is also named. The result is that it's easy for me to update/edit the config of my containers, but strangely the .Name variable accessible to the traefik template engine for the defaultRule of the docker provider prepends every container name with the name I setup as the "stack name" in portainer.

I'm not even sure I can find where traefik is fetchin this prefix that it uses in the .Name, probably the com.docker.compose.project label...

I'm really looking to understand how this .Name gets constructed by traefik... I will probably try to use the com.docker.compose.service label instead of .name, we'll see it that fixes my issue.

Ok, I finally found the solution that works for me, it was this:

 defaultRule = "Host(`{{ coalesce (index .Labels \"com.docker.compose.service\") (normalize .Name) }}.example.com`)"

So this is solved for me after a few months of waiting and finally a few hours digging what a sprig template is and what a "portainer stack" is..

Now looking into my next issue of making traefik access services that are not on the same docker network, probably will be solved trying out useBindPortIP or network, will use the one that gives me the most flexibility in creating my containers :wink:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.