Docker Community Forums

Share and learn in the Docker community.

Docker Swarm: Restrict 2 containers always deployed together on the same Node


My situation is I have my main service on container A and another helper service on container B. Container B is the sidecar of container A so I need container B will be deployed on the same Swarm node with container A.

How can I achieve this requirement?
I’m using a docker-compose.yml file to deploys my services, so it would be helpful if someone can give me an example with a docker-compose file.

Thanks in advance:smile:

Nothing changed since then: Use "affinity filter" in "docker service create"

The lagacy standalone Swarm did support affinity using env variables. The build in Swarm-mode does not. If you desperatly need affinity, use the davideshay’s workaround in the linked post or switch to a container orchestrator which actualy supports affinity like Kubernetes.

Thanks a lot for the information.