Search code examples
dockerdocker-swarm

How to limit number of replica of a service for a specific node?


Use case :

I have 2 nodes, a machineA with 1gb of RAM and a machineB with 4 gb of ram.

I deploy a service with 5 réplicas of Java container which use 1 gb of ram.

There will 1 be one Java container on machineA and 4 Java container on machine B.

If i shutdown machineB how can i prevent machineA to launch 4 Java ?

The machine doesn't hava enough RAM to launch these container and instead of improve availability it will decrease it.


Solution

  • This feature is not there as of now. There is a open issue for the same

    https://github.com/docker/swarm/issues/1734

    What you should do is add constraints of memory requirements. So if you add 500MB per container then that will prevent such a situation

    docker service create --name nginx --reserve-memory 500Mb --replicas 3 nginx