Search code examples
dockerdocker-composedocker-swarmdocker-swarm-mode

docker-compose deployment ensure container is installed on specific server


I have the following in my compose file:

  postgres:
    deploy:
      mode: global
      placement:
        constraints:
          - node.labels.server == data

And my node has the following when I inspect it:

"Spec": {
    "Labels": {
        "server": "data"
    },
    "Role": "worker",
    "Availability": "active"
},

However, when i deploy my stack postgres doesn't install anywhere...

What am I missing?

[UPDATE]

This is in the docker-compose.prod.yml btw in case you're wondering where the rest of the config is

[UPDATE]

This is running my compose as a stack across multiple servers. Using this command to fire it off:

docker stack deploy --compose-file docker-compose.yml --compose-file docker-compose.prod.yml stackname--with-registry-auth

Here is the output of ps:

root@sonatribe-1:~# docker service ps postgres --no-trunc
ID                  NAME                IMAGE               NODE                DESIRED STATE       CURRENT STATE       ERROR               PORTS

Elsewhere on other services I have stuff like this to ensure certain stuff is run on a manager node:

deploy:
  mode: global
  placement:
    constraints:
      - node.role == manager

And that works fine - it's only the one using:

node.labels.server == data

That doesn't work


Solution

  • This works for me, so I'll step through it to see if there's something missing on your end:

    I created a three node swarm, with one manager:

    docker node ls
    ID                            HOSTNAME            STATUS              AVAILABILITY        MANAGER STATUS      ENGINE VERSION
    diewo1v4bf2149fx422guh8ci *   dvc1                Ready               Active              Leader              18.06.1-ce
    jlz2sbman8wz7sukcs41jng1n     dvc2                Ready               Active                                  18.06.1-ce
    g7kocy0qo76m42o80phr6poh4     dvc3                Ready               Active                                  18.06.1-ce
    

    I add a label to node 3:

    docker node update dvc3 --label-add server=data
    

    docker node inspect dvc3:

    "Spec": {
                "Labels": {
                    "server": "data"
                },
                "Role": "worker",
                "Availability": "active"
            },
    

    I have a compose file stack.yml:

    version: '3.7'
    
    services:
      nginx:
        image: nginx:alpine
        deploy:
          mode: global
          placement:
            constraints:
              - node.labels.server == data
    

    I deploy the stack:

    docker stack deploy -c stack.yml test
    

    I was also running docker events on dvc1 which showed:

    2018-09-15T23:50:49.608191542-04:00 network create yf21vjsbjpcnastxugnp3dax5 (name=test_default)
    2018-09-15T23:50:49.609334207-04:00 network update yf21vjsbjpcnastxugnp3dax5 (name=test_default)
    2018-09-15T23:50:49.609992316-04:00 node update diewo1v4bf2149fx422guh8ci (name=dvc1)
    2018-09-15T23:50:49.610052739-04:00 node update g7kocy0qo76m42o80phr6poh4 (name=dvc3)
    2018-09-15T23:50:49.610086383-04:00 node update jlz2sbman8wz7sukcs41jng1n (name=dvc2)
    2018-09-15T23:50:50.067716009-04:00 service create xjgwq14ot13r1olzcsatd2ktb (name=test_nginx)
    2018-09-15T23:50:50.119799354-04:00 service update xjgwq14ot13r1olzcsatd2ktb (name=test_nginx)
    

    I should have also run events on dvc3 so I caught the task creation (which will only show on the node that gets it).

    Let's check the stack and task:

    docker service ls
    
    ID                  NAME                MODE                REPLICAS            IMAGE               PORTS
    xjgwq14ot13r        test_nginx          global              1/1                 nginx:alpine
    
    docker node ps dvc3
    ID                  NAME                                   IMAGE               NODE                DESIRED STATE       CURRENT STATE                ERROR               PORTS
    w1i3fpwfy88c        test_nginx.g7kocy0qo76m42o80phr6poh4   nginx:alpine        dvc3                Running             Running about a minute ago
    

    I hope that helps, can you dumb your solution down to that example and see if it works?