Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting an unhealthy docker container based on healthcheck

I am using Docker version 17.09.0-ce, and I see that containers are marked as unhealthy. Is there an option to get the container restart instead of keeping the container as unhealthy?

like image 340
Govind Kailas Avatar asked Nov 03 '17 03:11

Govind Kailas


People also ask

What happens when docker Healthcheck fails?

If a health check fails but the subsequent one passes, the container will not transition to unhealthy . It will become unhealthy after three consecutive failed checks. --timeout – Set the timeout for health check commands. Docker will treat the check as failed if the command doesn't exit within this time frame.

Why is my docker container unhealthy?

That unhealthy status is published as an event from Docker's API, so the platform running the container is notified and can take action to fix the application. Docker also records the result of the most recent health checks, which you can see when you inspect the container.

How do I restart a docker container?

Docker also lets the user set the restart policy upon exit or failure. Users can type docker ps to check if the restart policy is active; it will be shown as either Up , when the container is up and running, or Restarting when the container is in the restart state.

Does restarting docker restart containers?

A restart policy only takes effect after a container starts successfully. In this case, starting successfully means that the container is up for at least 10 seconds and Docker has started monitoring it. This prevents a container which does not start at all from going into a restart loop.


1 Answers

Restarting of unhealty container feature was in the original PR (https://github.com/moby/moby/pull/22719), but was removed after a discussion and considered to be done later as enhancement of RestartPolicy.

At this moment you can use this workaround to automatically restarting unhealty containers: https://hub.docker.com/r/willfarrell/autoheal/

Here is a sample compose file:

version: '2' services:   autoheal:     restart: always     image: willfarrell/autoheal     environment:       - AUTOHEAL_CONTAINER_LABEL=all     volumes:       - /var/run/docker.sock:/var/run/docker.sock 

Simply execute docker-compose up -d on this

like image 62
Navrocky Avatar answered Sep 24 '22 21:09

Navrocky