From the docs, I could only find information about placement restrictions based on node attributes, likes labels and its role. However, I couldn't find how to configure placement based on the services already running on a given node.

How can I do that?

  • Why can't your two services be run on the same node? There may be other ways to achieve your goal.
    – BMitch
    Oct 8, 2018 at 21:22
  • I want different IPs for similar microservices with different parameters, so replicas won’t do. The question is interesting regardless
    – ivarec
    Oct 8, 2018 at 21:25
  • Each container gets it's own private internal IP by default, even when on the same host. And if you publish with ingress, a port will be opened on every host of the network. The answer to the question asked is "no" so I'm trying to see if there's a way to solve the underlying problem.
    – BMitch
    Oct 8, 2018 at 21:29
  • Thanks for the input! If it’s a no, it’s a useful answer regardless
    – ivarec
    Oct 8, 2018 at 21:30

1 Answer 1


At present the answer is "no", swarm mode does not support affinity or anti-affinity between a service and other containers/services running on a node. However, depending on your goal there are other options.

One option is to reserve resources like CPU and memory for your container. This will ensure two resource intensive applications do not schedule themselves on the same node if there are insufficient resources.

The other option, as you've mentioned, is to use labels and constraints. You can give the same label to multiple nodes, which is often done to specify a type of node, or assign nodes to a specific team/project/environment.

Both of these options will reduce your fault tolerance, particularly if you assign a label to one or a small handful of nodes. If those nodes with the label go down, docker will be unable to reschedule your service on another node.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.