Sreekanth R
Sreekanth R

Reputation: 143

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?

Upvotes: 6

Views: 11985

Answers (2)

BMitch
BMitch

Reputation: 263479

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/

Upvotes: 5

atline
atline

Reputation: 31574

There is no auto restart mechanism for unhealth container currently, see this, but you can make a workaround as mentioned here:

docker run -d \
    --name autoheal \
    --restart=always \
    -e AUTOHEAL_CONTAINER_LABEL=all \
    -v /var/run/docker.sock:/var/run/docker.sock \
    willfarrell/autoheal

It add docker unix domain socket to the monitor container, then it could monitor all unhealthy container and restart it for you if other container is not healthy.

Upvotes: 5

Related Questions