Hi,
Is there a way to use affinity constraints to have docker swarm launch a container only a docker host that does not already have the same container already running on?
Share and learn in the Docker community.
Hi,
Is there a way to use affinity constraints to have docker swarm launch a container only a docker host that does not already have the same container already running on?
Yes you can:
Docker Swarm Mode?
Docker Swarm Mode or Docker Swarm “classic”?
affinity:container!=~your-service-name*
This would be Docker Swarm Mode but then --mode global will limit us to being able to scale only up to the # of docker hosts.
It was either one of those two approaches, depending on what you want
Hi,
I am using the latest swarm, and the “affinity:container!=~your-service-name*” approach does not work for me.
I am using the below command to create service:
docker service create --name consul-boot -e ‘affinity:container!=~consul*’ …
whether the “docker service create” support specifying “affinity”?
Thanks!
Found this thread while working a similar situation, I’ll add what I found.
What I’m experiencing in a similar situation is that --mode global starts one container per node, and it does honor --constraint ‘node.hostname != dontrunonnodename’. Note though that when you create the service it looks like it want’s to run all nodes, including the != node, but once the service comes up, no container is started on the != node. You’ll see this if you docker ps on the constrained node. You get a container Allocated, but not Running.
Looks kinda like this:
masternode# docker service create --with-registry-auth --name worker --mode global --constraint ‘node.hostname != masternode’ --network overlay-net worker
masternode# docker service ps worker
ID NAME IMAGE NODE DESIRED STATE CURRENT STATE ERROR
ekmn0qpnp6yt9yejc3sv77f7e worker worker workernode1 Running Running 11 minutes ago
4fgtppcmygdl83in0q19sk8gg _ worker worker workernode2 Running Running 11 minutes ago
d6ilju6iiq4la64k6973n38oc _ worker worker masternode Running Allocated 12 minutes ago
masternode# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
1d1d3ddae487 master:latest “dostuff” 16 minutes ago Up 16 minutes master