Search code examples
dockerdocker-composedocker-swarm

how to set the service mode when using docker compose?


I need to set service mode to global while using compose files .

Any chance we can use this in compose file ? I have a requirement where for a service there should be exactly one container on every node/host .

This doesn't happen with "spread strategy" of swarm if a node goes down & comes up , it just attains the equal number of containers on each host irrespective of services .

https://github.com/docker/compose/issues/3743


Solution

  • We can do this easily now with docker compose v3 (version 3) under the deploy(mode) section.

    Prerequisites -

    docker compose version should be 1.10.0+

    docker engine version should be 1.13.0+

    Example compose file -

    version: "3"
    
      services:
        nginx:
          image: nexus3.example.com/prd-nginx-sm:v1
          ports:
            - "80:80"
          networks:
            - cheers
          volumes:
            - logs:/rest/out/
          deploy:
            mode: global
            labels:
              feature.description: "Frontend"
            update_config:
              parallelism: 1
              delay: 10s
            restart_policy:
              condition: any
          command: "/usr/sbin/nginx"
    
    networks:
      cheers:
    
    volumes:
      logs:
      data:
    

    Deploy the compose file -

    $ docker stack deploy -c sm-deploy-compose.yml --with-registry-auth CHEERS

    This will deploy nginx container on all the nodes participating in the cluster .