Docker container HEALTHCHECK refers to using HEALTHCHECK instruction in Dockerfile to check the operating STATUS of the container and displaying healthy/unhealthy in the STATUS bar of Docker ps.

The HEALTHCHECK directive has two formats:

  • HEALTHCHECK [OPTIONS] CMD command(Check container health by running commands inside the container)
  • HEALTHCHECK NONE(Disable inheriting any health checks from the underlying image)

The HEALTHCHECK directive tells Docker how to test a container to check that it is still working. This can detect situations such as a Web server stuck in an infinite loop and unable to process new connections even though the server process is still running.

When a container has a health check specified, it has a healthy state in addition to its normal state. The initial state is starting. Once the health check is passed, it will return to healthy (no matter what state it was in before). After a certain number of consecutive failures, it becomes unhealthy.

The options available before CMD are:

  • –interval=DURATION (default: 30s)
  • –timeout=DURATION (default: 30s)
  • –start-period=DURATION (default: 0s)
  • –retries=N (default: 3)

The health check is run first at interval seconds after the container is started, and then again at interval seconds after the previous check is complete.

If a status check takes longer than timeout seconds, the check is considered to have failed.

A container is considered unhealthy only when its running state check fails retries several times.

Start period Specifies the initialization time for containers that need time to start. Probe failures during this period will not count toward the maximum retry count. However, if the health check succeeds during startup, the container is considered started, and all consecutive failures are counted to the maximum number of retries.

There can only be one HEALTHCHECK directive in a Dockerfile. If more than one is listed, only the last HEALTHCHECK will take effect.

The command following the CMD keyword can be a shell command (for example HEALTHCHECK CMD /bin/check-running) or an exec array (as with other Dockerfile commands, see ENTRYPOINT for details).

The exit status of command indicates the health status of the container. Possible values are:

  • 0: successful – the container is healthy and ready to use
  • 1: Unhealthy – The container does not work properly
  • 2: Reserved — do not use this exit code

For example, check every five minutes or so to see if the Web server can serve the site’s home page in three seconds:

HEALTHCHECK --interval=5m --timeout=3s \
  CMD curl -f http://localhost/ || exit 1
Copy the code

To help debug failed probes, any output text (utF-8 encoded) that Command writes on stdout or stderr is stored in the health state and can be queried via Docker Inspect. Such output should be kept short (currently only the first 4096 bytes are stored).

When the health state of the container changes, a HEALTH_STATUS event is generated with the new state.

Original link: k8scat.com/posts/docke…