Getting Started with Node Labels In Docker Swarm
- List your current nodes.
docker node ls
- Add a label to a node.
docker node update --label-add availability_zone=east <NODE_NAME> docker node update --label-add availability_zone=west <NODE_NAME>
- View existing labels with:
docker node inspect --pretty <NODE_NAME>
- You can useÂ
--constraint
 when creating a service to restrict which nodes will be used to execute a service’s tasks.docker service create --name nginx-east --constraint node.labels.availability_zone==east --replicas 3 nginx docker service ps nginx-east docker service create --name nginx-west --constraint node.labels.availability_zone!=east --replicas 3 nginx docker service ps nginx-west
- UseÂ
--placement-pref
 to spread tasks evenly based on the value of a specific label.docker service create --name nginx-spread --placement-pref spread=node.labels.availability_zone --replicas 3 nginx docker service ps nginx-spread
Tag:Docker Swarm