Docker Container health check refers to Dockerfile Use in
HEALTHCHECK The command checks the running state of the container ,
docker ps Of STATUS Column display healthy/unhealthy.
HEALTHCHECK Instructions come in two formats ：
HEALTHCHECK [OPTIONS] CMD command（ Check the health of the container by running commands inside the container ）
HEALTHCHECK NONE（ Disable inheriting any health checks from the underlying image ）
HEALTHCHECK Instructions tell Docker How to test a container , To see if it's still working .
This can detect something , for example web The server is in an infinite loop , Unable to process new connection , Even if the server process is still running .
When the container specifies a health check , In addition to the normal state , It's also healthy . This state is initially
As long as the health check-up passes , It will return to
healthy（ No matter what state it was in before ）. After a certain number of consecutive failures , It will become
CMD The options that can appear before are ：
- --interval=DURATION（ Default ：30s）
- --timeout=DURATION（ Default ：30s）
- --start-period=DURATION（ Default ：0s）
- --retries=N（ Default ：3）
The runtime check will first be performed after the container is started
interval Run in seconds , And then after the last check
interval Run again in seconds .
If a status check takes more than
timeout second , The inspection failed .
The running state check of the container failed continuously
retries It's not healthy until it's too late .
start period Provide initialization time for containers that need time to start . Detection failures during this period are not counted in the maximum number of retries .
however , If the health check is successful during startup , The container is considered to be started , The maximum number of retries will be calculated for all consecutive failures .
Dockerfile There can only be one of them
HEALTHCHECK Instructions . If more than one is listed , Only the last one
HEALTHCHECK Will take effect .
CMD The command after the keyword can be shell command （ for example
HEALTHCHECK CMD /bin/check-running） or exec Array （ And others Dockerfile command , For more information , Please see the
command The exit state of represents the health state of the container . The possible value is ：
- 0： success -- The container is working well , have access to
- 1： unhealthy —- The container is not working properly
- 2： Retain -- Do not use this exit code
for example , Check every five minutes or so web Whether the server can serve the home page of the site in three seconds ：
HEALTHCHECK --interval=5m --timeout=3s \ CMD curl -f http://localhost/ || exit 1
To help debug failed probes ,
command Written in stdout or stderr Any output text on （UTF-8 code ） Will be stored in a healthy state , And through
docker inspect The query .
Such output should be kept short （ At present, only the previous 4096 Bytes ）.
When the health of the container changes , Will generate a... With a new state
health_status event .
Link to the original text ：https://goworker.cn/posts/docker-container-healthcheck/