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.

services:
  angularDocs:
    image: artifactory.company.com/containers/angular-docs:branchName
    networks:
      - traefik-net
    labels:
      - "traefik.http.routers.angularDocs.rule=Host(`branchName.angular.swarm.company.com`)"
      - "traefik.http.services.angularDocs.loadbalancer.server.port=80"

networks:
  traefik-net:
    external: true

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

docker service create --options artifactory.company.com/containers/angular-docs:branchName 

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

docker service create --name angulardocs  --label "traefik.http.routers.angulardocs.rule=Host(`branchName.angular.swarm.company.com`)" --label "traefik.http.services.angulardocs.loadbalancer.server.port=80" --network traefik-net artifactory.company.com/containers/angular-docs:branchName 

In traefik 2.0 i get a route

Host( angulardocs-1-egyumq4nfke1lz3ohya38mwsj )

instead of Host( v8.angular.company.com )

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": "artifactory-ch.company.com/company.containers/angular-docs:SIXKUFW-5647",
                "com.docker.stack.namespace": "SIXKUFW-5647"
            },
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "artifactory-ch.company.com/company.containers/angular-docs:SIXKUFW-5647@sha256:fa2fd9f0eec0e170c19a6a786d9f6657ec99dd1480c8ebd978c3b084ad47a6e7",
                    "Labels": {
                        "com.docker.stack.namespace": "SIXKUFW-5647",
                        "traefik.http.routers.angularDocs.rule": "Host(`v8.angular.lx-ch25.company.com`)",
                        "traefik.http.services.angularDocs.loadbalancer.server.port": "80"
                    },

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


"Spec": {
            "Name": "angulardocs",
            "Labels": {
                "traefik.http.routers.angulardocs.rule": "Host('v8.docs.lx-ch25.company.com')",
                "traefik.http.services.angulardocs.loadbalancer.server.port": "80"
            },
            "TaskTemplate": {
                "ContainerSpec": {
                    "Image": "artifactory-ch.company.com/company.containers/angular-docs:SIXKUFW-5647@sha256:fa2fd9f0eec0e170c19a6a786d9f6657ec99dd1480c8ebd978c3b084ad47a6e7",
                    "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(\`v8.docs.lx-ch25.company.com\`)" --container-label "traefik.http.services.angulardocs.loadbalancer.server.port=80" --network traefik-net artifactory-company.com/company.containers/angular-docs:SIXKUFW-5647

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.

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

version: "3.3"

services:
  traefik:
    image: "traefik:v2.0.1"
    command:
      - --entrypoints.web.address=:80
      - --providers.docker
      - --api.insecure
      - --entrypoints.jnlp.address=:50000
    ports:
      - "80:80"
      - "8080:8080"
      - "50000:50000"
    volumes:
      - "/var/run/docker.sock:/var/run/docker.sock:ro"
    networks:
      - traefik-net
    labels:
      - "traefik.http.routers.traefik.rule=Host(`traefik.lx-ch25.company.com`)"
      - "traefik.http.routers.traefik.loadbalancer.server.port=8080"

networks:
  traefik-net:
    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:

--providers.docker.swarmMode=true

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