Restart all containers when one is unhealthy


We are using healthchecks in Docker compose files ( version 3.4 ). We run Docker stack deploy in a single host swarm mode. If one container is unhealthy we would like to restart all containers. Is there any way to do this in an automated way, Docker swarm mode only restarts the unhealthy container?


Remove the stack and redeploy.

docker stack rm stackname
docker stack deploy -c compose-file stackname

Hi, Thanks
I assume you mean we need our own script that monitors the events and then calls the API to do this. We were wondering whether there was any settings in the compose file or something that was all.

Sorry, I missed that part of ‘automated’.

On my own, I cannot assert there is no such setting in compose file for this. If it were, I’d expect it to be in the compose reference document. You don’t say whether you read that or not.

[I admit to having a limited imagination. I wonder why the healthy parts of a cluster should restart to cause recover of the unhealthy one. Is there something else you are after?]

:triangular_flag_on_post: This solution risks to contradict one of the major benefits with cloud and multiple containers. The term “pets vs. cattle” attempts to describe how large applications used to be treated as “pets” (“I always make sure my pet is alive and well”) whereas today’s applications may be distributed across many containers and therefore treated as cattle (“There are so many, I don’t care if a few die”).
If you kill all your containers if only one is misbehaving, you might end up in the old “pet” way of thinking.
Just my 2 ¢ :slight_smile:

Hi, Point taken. I think we need to define better healthchecks per container

@hannesmvw warnings aside, just to answer the question on its technical face:

A process can listen to docker events and run a script that restarts services when a container is unhealthy. The script can use ‘docker service update --force ${servicename}’