6

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?

1
  • Your docker run command appears to be invalid. The net option needs to be --net rather than -net, and host networking is incompatible with publishing ports. Commented Aug 17, 2019 at 10:47

2 Answers 2

5

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/

Sign up to request clarification or add additional context in comments.

Comments

5

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.