I'm using Docker Swarm and I've got some containers that I would prefer to have co-located on the same node. For example, each of my services has its own Caddy proxy container following a sidecar pattern, and several services utilize Redis caches. If my server container is running on Node1 but the Redis container is on Node2, it creates inefficiencies and unnecessary latency.
I want to avoid reconfiguring all my existing containers to integrate Caddy and Redis, as that would mean constant updates and maintenance. Additionally, I'd prefer not to set hostnames as placement constraints because I want to utilize the resilience of having multiple nodes, allowing them to tolerate failures. Currently, I've resorted to using hostname constraints, but I feel like there should be a better solution! I couldn't find much online, and I remember Docker mentioned back then that co-location wasn't a feature 'yet.' I'd love to hear if anyone has tackled something similar. What I'm really looking for is a way to tell the Swarm scheduler: 'I don't care which node you place these services on, as long as they're both on the same node, and they can shift to another if a node goes down.'
1 Answer
Docker by itself isn't clustered, and everything typically runs on one node unless you're using Docker Swarm or Kubernetes. While you're using Swarm, you might be looking for what’s called 'affinity' in Kubernetes, which I’m not sure Swarm has. Although it could be mimicked with placement constraints, that could be tricky in your case.
There is a way to control service placement in Docker Swarm. You can check out the Docker documentation on that, which might help clarify how to set it up!
Ahh, I see! I forgot to mention that I'm definitely using Swarm with three nodes. I've edited my post to clarify. I know about placement constraints, like specifying `node.hostname == node01`, but if Node01 fails, it won't automatically move the services to Node02. I'm trying to figure out if there's a way to apply a constraint like `node.hostname == [serviceX].hostname` that ensures both services are on the same node without locking them down to a specific one.