Deploy docker servce to swarm with traefik 2.0 using CLI

I have a docker swarm with a network network name: traefik-net type: overlay scope: swarm

Then i have a docker compose that works.

      - traefik-net
      - "traefik.http.routers.angularDocs.rule=Host(``)"
      - ""

    external: true

But instead of creating a compose file for each container image. I would like to do

docker service create --options 

but when i try like this, it does not work with traefik

docker service create --name angulardocs  --label "traefik.http.routers.angulardocs.rule=Host(``)" --label "" --network traefik-net 

In traefik 2.0 i get a route

Host( angulardocs-1-egyumq4nfke1lz3ohya38mwsj )

instead of Host( )

It is like the the labels are configured wrong compared to when labels are in the compose file.

I hope somebody has an answer.

As a side note, i was able to it in traefik 1.x

HI @RasmusKVoss

This look like the defaultRoute from the docker provided. So yes I think as you conclude the labels are incorrect.

docker service inspect angulardocs | jq .[].Spec.Labels will show you what is set.

The shell is interpreting the ` ` as Command Substitution. Surround your label definition with single quotes ' instead.

Thank you for trying to answer my question.

Must have been a copy paste mistake on the question as the commands i executed was with regular single quotes.

If i deploy with the compose file i get
docker service inspect SIXKUFW-5647_angularDocs

"Spec": {
            "Name": "SIXKUFW-5647_angularDocs",
            "Labels": {
                "com.docker.stack.image": "",
                "com.docker.stack.namespace": "SIXKUFW-5647"
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "",
                    "Labels": {
                        "com.docker.stack.namespace": "SIXKUFW-5647",
                        "traefik.http.routers.angularDocs.rule": "Host(``)",
                        "": "80"

If i deploy with command line i get
docker service inspect angulardocs

"Spec": {
            "Name": "angulardocs",
            "Labels": {
                "traefik.http.routers.angulardocs.rule": "Host('')",
                "": "80"
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "",
                    "Init": false,
                    "StopGracePeriod": 10000000000,
                    "DNSConfig": {},
                    "Isolation": "default"

It is like the traefik labels is in the wrong location.

They are. Using swarm mode the labels need to nest under deploy: in the compose file.

The labels are wrong for me only when i used command line,
Perhaps it is important to know that i use

docker stack deploy -c compose-file SIXKUFW-5647

To deploy it to the swarm, when using a compose-file

I have solved it.

I had to use --container-label

On top of that i actually had to use the execution quotes, and i had to escape them, now it works perfectly.

docker service create --name angulardocs  --container-label "traefik.http.routers.angulardocs.rule=Host(\`\`)" --container-label "" --network traefik-net

Thank you for getting me on the right path with the labels comment

I guess that your traefik is not in swarm mode then?

You loose the benefit of updating a service label and NOT having the containers restart. Not the approach I would take.

Honestly I copied a docker compose with traefik and that might be the problem.

version: "3.3"

    image: "traefik:v2.0.1"
      - --entrypoints.web.address=:80
      - --providers.docker
      - --api.insecure
      - --entrypoints.jnlp.address=:50000
      - "80:80"
      - "8080:8080"
      - "50000:50000"
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
      - traefik-net
      - "traefik.http.routers.traefik.rule=Host(``)"
      - "traefik.http.routers.traefik.loadbalancer.server.port=8080"

    external: true

So you are telling, that i am missing a flag in my docker compose. ?

If traefik is in swarm mode i would be able to just us the --label instead of --container-label.

Yes for swarm mode:


Then docker service --labels would be used yes.
If using a compose for stack deploy then labels must go under the deploy: key.

Awesome thank you : )

1 Like