Search code examples
dockerdocker-composedocker-machinedocker-swarm

Docker Swarm - Schedule container on each machine


I'm currently trying to spawn a container instance of Redis separately on each cluster machine using Docker Swarm/Compose. I'm currently using 4 machines for the example. Using the affinity labels, the idea was to make sure two redis instances wouldn't get scheduled on to the same machine.

At first, the following worked.

redis:
  image: redis
  environment:
    AFFINITY: com.myself.name!=redis
  labels:
    - "com.myself.name=redis"

However after forcing some containers to stop to simulate crashes, pushing them back, turning all containers off, and then finally running everything from the start again using docker-compose scale redis=4, I find redis running two containers on the same machine even though there's 4 available in the cluster.

What exactly am I doing wrong? Thanks.


Solution

  • You should try this:

    redis:
      image: redis
      environment:
        - "affinity:container!=*redis*"
      labels:
        - "com.myself.name=redis"
    

    This should work!

    Please, let me know