Restarting an unhealthy docker container based on healthcheck

前端 未结 6 954
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-30 16:31

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 cont

6条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-30 16:54

    You can restart automatically an unhealthy container by setting a smart HEALTHCHECK and a proper restart policy.

    The Docker restart policy should be one of always or unless-stopped.

    The HEALTHCHECK instead should implement a logic that kills the container when it's unhealthy.

    In the following example I used curl with its internal retry mechanism and piped it (in case of failure/service unhealthy) to the kill command.

    HEALTHCHECK --interval=5m --timeout=2m --start-period=45s \
       CMD curl -f --retry 6 --max-time 5 --retry-delay 10 --retry-max-time 60 "http://localhost:8080/health" || bash -c 'kill -s 15 -1 && (sleep 10; kill -s 9 -1)'
    

    The important step to understand here is that the retry logic is self-contained in the curl command, the Docker retry here actually is mandatory but useless. Then if the curl HTTP request fails 3 times, then kill is executed. First it sends a SIGTERM to all the processes in the container, to allow them to gracefully stop, then after 10 seconds it sends a SIGKILL to completely kill all the processes in the container. It must be noted that when the PID1 of a container dies, then the container itself dies and the restart policy is invoked.

    • kill docs: https://linux.die.net/man/1/kill
    • curl docs: https://curl.haxx.se/docs/manpage.html
    • docker restart docs: https://docs.docker.com/compose/compose-file/compose-file-v2/#restart

    Gotchas: kill behaves differently in bash than in sh. In bash you can use -1 to signal all the processes with PID greater than 1 to die.

提交回复
热议问题