docker container HEALTHCHECK health check

Health docker containers is detected when writing dockerfile, dockerfile written to the detection mechanism based on this generated image docerfile, health monitoring function will be run when the container.

dockerfile in the format:

  • HEALTHCHECK [Options] CMD <command>: Check the settings vessel health command.
  • HEALTHCHECK NONE: If there is a health check command base image, this line can be masked using their health check instruction.

HEALTHCHECK instruction is to tell how Docker engine should determine whether a state of normal container, which is introduced by Directive Docker 1.12.

In the absence of HEALTHCHECK instruction, Docker engine can only be judged by whether the container is in an abnormal state in the container main process exits. In many cases this is not a problem, but if the program locks up or an infinite loop, the application process does not quit, but the container has been unable to provide the service. In the previous 1.12, Docker will not detect this state of the container, so as not to reschedule, resulting in part of the container may have been unable to provide the service are still accepting user requests.

And since 1.12, Docker provided HEALTHCHECK instruction the instruction specified by a single command, whether the command line is determined by the main process vessel normal service state further, to compare the actual state of the real reaction vessel.

When an image is specified in the instruction HEALTHCHECK, with its starting container, the initial status of Starting, after checking becomes successful HEALTHCHECK Healthy instruction, if a certain number of consecutive failures will become unhealthy.

HEALTHCHECK supports the following options:

  • --interval = <interval>: Two health check interval, default is 30 seconds;
  • --timeout = <duration>: Health Check command to run the timeout, if more than this time, this health check was considered a failure, default 30 seconds;
  • --retries = <number>: When a specified number of consecutive failures, the state will be considered Unhealthy container, the default three times.

And CMD, ENTRYPOINT the same, HEALTHCHECK can appear only once, if you write more, only the last one will work.

In HEALTHCHECK [Options] CMD subsequent commands, format and ENTRYPOINT same format divided into shell, and exec format. The return value of the command determines the success of the sub-health check or not: 0: Success; 1: Failure; 2: Reserved, do not use this value.

Usage example:

[root@node02 test]# cat Dockerfile      #Dockerfile文件如下
FROM nginx:latest
COPY test.txt /test.txt
HEALTHCHECK --interval=5s --timeout=3s CMD cat /test.txt || exit 1 

Here we set up inspected once every 5 seconds (here for a very short interval test so, should actually be relatively long), if the health check command did not respond to more than three seconds as a failure, and the use of CMD cat /test.txt || exit 1 as a health check command.

Construction of the mirror:

[root@node02 test]# docker build -t lzj:v6 .

Start a container:

[root@node02 test]# docker run -d --name web03 lzj:v6

When the container is running, you can view the operating state of the container, the initial state (health: starting), when a detection is successful, is converted to (Healthy), as follows:

docker container HEALTHCHECK health check

If a health check consecutive failures exceeded the number of retries, the status will change to (unhealthy). I'm here to enter the vessel will be deleted CMD execution of its view test.txt file state will be as unhealthy, as follows:

docker container HEALTHCHECK health check

To help troubleshoot, health check output commands (including stdout and stderr) will be stored in a healthy state, you could use docker inspect to see.

docker container HEALTHCHECK health check

-------- end of this article so far, thanks for reading --------

Guess you like

Origin blog.51cto.com/14154700/2464362