Answers: <p>If you don't want the containers to restart, you can set the <code>condition:</code> to <code>none</code>. See <a href="https://docs.docker.com/compose/compose-file/#restart_policy" rel="nofollow noreferrer">https://docs.docker.com/compose/compose-file/#restart_policy</a></p>
  
  <p>On the swarm manager, you could use <code>docker stack ps</code> to check the status of the containers on a regular basis and shut down the stack if the database containers aren't running (using cron or whatever). For example:</p>
  
  <p><code>docker stack ps --filter "desired-state=Running" your-stack |grep database || docker stack rm your-stack</code>.</p>
  
  <p>If containers are not able to restart, they shouldn't show up in the list once they fail, and once none are found, you can remove the stack.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-12-19T16:07:24.017Z
Answers: 2018-01-23T17:24:01.030Z
Answers: 2018-01-23T17:24:01.030Z
Answers: Andrew Schlei
Answers: <p>Using Docker's healthcheck feature (a side process for an additional script) you could register and sync your container health status with an external service e.g. Consul. The API of this service would then allow further services to take some action, for example as you said redeploy the whole stack or raise an issue.</p>
  
Answers: CC BY-SA 3.0
Answers: 2017-11-23T10:23:12.973Z
Answers: 2017-11-23T10:23:12.973Z
Answers: Ta Mu
Body: <p>Say I have several docker services using swarm defined as such:</p>
  
  <pre><code>version : '3'
  services:
      server:
          image : my_server:latest
          deploy: 
              replicas: 5
              restart_policy: any
      database:
          image : my_db:latest
          deploy: 
              replicas: 5
              restart_policy: any
      logs:
          image : my_logger:latest
          deploy: 
              replicas: 1
              restart_policy: any
  </code></pre>
  
  <p>I then deploy this as a docker-stack as such. The services are <strong>server</strong>, <strong>database</strong> and <strong>logs</strong>. I make great use of the network abstractions provided by docker swarm, but my failure-case is a bit undockerish.</p>
  
  <p>*In this case when all containers in <strong>database</strong> fail, then I want the <em>entire</em> stack to be shutdown or otherwise made unavailable*. Is there any built-in way to accomplish this task? Does the concept of a stack exist in a way for the individual services to access?</p>
  
  <p>This question can be alternatively answered with a generalization : "How, with built-in docker features, can I be made aware of container failure"? Because I'm assuming there is no way to do exactly what I am asking.</p>
  
Comments: CC BY-SA 3.0
Comments: 2017-11-22T22:42:40.273Z
Comments: Could you remove the superfluous tags? According to me this question is about `docker-swarm` and not about docker and docker-compose.
Comments: 030
Tags: <docker><docker-compose><docker-swarm>
Title: How to Shutdown Docker Stack on Container Failure
UserDisplayName: Nyarmith
ThreadText: # How to Shutdown Docker Stack on Container Failure
  
  (Asked by: username_0 on 2017-11-02)
  
  Say I have several docker services using swarm defined as such:
  
  ```
  version : '3'
  services:
      server:
          image : my_server:latest
          deploy: 
              replicas: 5
              restart_policy: any
      database:
          image : my_db:latest
          deploy: 
              replicas: 5
              restart_policy: any
      logs:
          image : my_logger:latest
          deploy: 
              replicas: 1
              restart_policy: any
  
  ```
  
  I then deploy this as a docker-stack as such. The services are **server**, **database** and **logs**. I make great use of the network abstractions provided by docker swarm, but my failure-case is a bit undockerish.
  
  \*In this case when all containers in **database** fail, then I want the *entire* stack to be shutdown or otherwise made unavailable\*. Is there any built-in way to accomplish this task? Does the concept of a stack exist in a way for the individual services to access?
  
  This question can be alternatively answered with a generalization : "How, with built-in docker features, can I be made aware of container failure"? Because I'm assuming there is no way to do exactly what I am asking.
  
  **Tags:** docker, docker-compose, docker-swarm
  
  > **Comments:**
  > **username_1**: Could you remove the superfluous tags? According to me this question is about `docker-swarm` and not about docker and docker-compose.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2017-12-19. Score: 1
  
  If you don't want the containers to restart, you can set the `condition:` to `none`. See <https://docs.docker.com/compose/compose-file/#restart_policy>
  
  On the swarm manager, you could use `docker stack ps` to check the status of the containers on a regular basis and shut down the stack if the database containers aren't running (using cron or whatever). For example:
  
  `docker stack ps --filter "desired-state=Running" your-stack |grep database || docker stack rm your-stack`.
  
  If containers are not able to restart, they shouldn't show up in the list once they fail, and once none are found, you can remove the stack.
  
  
  ---
  
  ### Answer by username_3 on 2017-11-23. Score: 0
  
  Using Docker's healthcheck feature (a side process for an additional script) you could register and sync your container health status with an external service e.g. Consul. The API of this service would then allow further services to take some action, for example as you said redeploy the whole stack or raise an issue.
  
  
  ---
  
  