If you wanna choose a limited numbers of containers in a specific node, you need to set: docker node update --availability drain $NODE_NAME
You will set drain for all nodes and just 1 as --availability active . If you have just one node as active, the containers will be create just in that node but if you have or more as active, the docker swarm will share the containers for all hosts.
For example:
docker node update --availability active worker1 (don`t need set it, active is the default availability)
docker node update --availability drain worker2
docker node update --availability drain worker3
And now you can use: $ docker service create --replicas 3 -- name database db:latest
and you will have:
manager
worker 1 (node 1)
3 containers (db)
worker 2 (node 2)
0 containers (apps)
worker 3 (node 3)
0 containers (apps)
then set:
docker node update --availability drain worker1
docker node update --availability active worker2
docker node update --availability active worker3
And now you can use: $ docker service create --replicas 20 -- name my_app app:latest .
Docker swarm will share 10 containers for each node with the same app.
When everything is ok, set --availability active for all nodes.