Search code examples
dockerhealth-monitoring

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?


Solution

  • 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 restart unhealthy 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