6

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.

  • Could you remove the superfluous tags? According to me this question is about docker-swarm and not about docker and docker-compose. – 030 Nov 22 '17 at 22:42
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.

0

As to this Github issue indicates that:

depends_on is a no-op when used with docker stack deploy. Swarm mode services are restarted when they fail, so there's no reason to delay their startup. Even if they fail a few times, they will eventually recover.

the answer to the question:

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?

is that it is impossible as one of the features of container orchestration technologies like docker-swarm is auto restart. This means that a container is restarted automatically when it was shutdown. So if the database container goes down, it will be restarted automatically.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.