Search code examples
dockerdockerfiledocker-engine

unhealthy docker container not restarted by docker native health check


I have implemented docker native health check by adding HEALTHCHECK command in Docker file as shown below,

HEALTHCHECK --interval=60s --timeout=15s --retries=3 CMD ["/svc/app/healthcheck/healthCheck.sh"]

set the entry point for the container

CMD [".././run.sh"] 

executing the docker run command as shown below,

docker run -d  --net=host --pid=host --publish-all=true -p 7000:7000/udp applicationname:temp

healthCheck.sh is exiting with 1, when my application is not up and I can see the container status as unhealthy, but it is not getting restarted.

STATUS

Up 45 minutes (unhealthy)

Below are the docker and OS details:

[root@localhost log]# docker -v
Docker version 18.09.7, build 2d0083d

OS version

NAME="CentOS Linux"
VERSION="7 (Core)"

How to restart my container automatically when it becomes unhealthy?


Solution

  • Docker only reports the status of the healthcheck. Acting on the healthcheck result requires an extra layer running on top of docker. Swarm mode provides this functionality and is shipped with the docker engine. To enable:

    docker swarm init
    

    Then instead of managing individual containers with docker run, you would declare your target state with docker service or docker stack commands and swarm mode will manage the containers to achieve the target state.

    docker service create -d  --net=host applicationname:temp
    

    Note that host networking and publishing ports are incompatible (they make no logical sense together), net requires two dashes to be a valid flag, and changing the pid namespace is not supported in swarm mode. Many other features should work similar to docker run.

    https://docs.docker.com/engine/reference/commandline/service_create/