How can I deploy two (or more) containers on a same node using affinity filter on docker create service?
I’ve tried the following commands but both containers get deployed on different nodes.
For 1st container:
docker service create -d -p 4000:80 --name myhello username/repo:part1
For 2nd container:
docker service create -d -p 8888:5000 --name myflask -e affinity:service==myhello username/repo:part2
docker service create -d -p 8888:5000 --name myflask -e affinity:container==‘containerNo’ username/repo:part2
Could someone possibly give me some hints or route me in the right direction ?
Thanks in anticipation.
Docker Swarm does only provide placement preferences, which areused to evenly distribute replicas over locations. Actualy this is more the oposite of afinity cough
There is a terrible workaround, though: label your nodes and use the node labels as placement constraints in your service. The terrible part beeing: if the node is down your services won’t be recrated on a different node.
It is a pitty that only node and engine labels can be used as placement constraints. If placement constraint would allow container or service labels, affinity would be possible…
Thanks a lot. It worked as expected.
However I’m new into docker so I would like to clarify one thing. I followed docker main page and found relevant info about affinity filter in Swarm. I followed an old Youtube video as well, where using the docker run command, affinity is possible and it shows on which node the app is deployed(using docker ps command). However when I use docker run, the app is deployed but I cannot see on which node it is running using docker ps command. Does it even deploy on any of the swarm node with docker run or its only possible with docker create service?
docker run creates a container on the local docker engine.
docker service create creates a service, which will be scheduled as a task on a swarm node, which eventually creates the container.
Okay. Thanks for your help.
From what I could tell the affinity filter in swarm went away at some point and is not currently available.
I’ve found a few use cases where this would be valuable, so I’ve created a way to simulate swarm affinity of services with each other on the same node via docker label placement constraints. See this docker hub container and associated github repository: