The HEALTHCHECK instruction

Any Docker container is designed to run just one process/application/service as a best practice and also to be uniquely compatible with the fast-evolving Microservices Architecture (MSA). The life cycle of a container is tightly bound to the process running inside the container. When the process running inside the container crashes or dies for any reason, the Docker Engine will move the container to the stop state. There is a possibility that the application running inside the container might be in an unhealthy state and such a state must be externalized for effective container management. Here the HEALTHCHECK instruction comes in handy to monitor the health of the containerized application by running a health monitoring command (or tool) at a prescribed interval.

The syntax of the HEALTHCHECK instruction is as follows:

HEALTHCHECK [<options>] CMD <command> 

Here, the code terms mean the following:

  • <command>: The HEALTHCHECK command is to be executed at a prescribed interval. If the command exit status is 0, the container is considered to be in the healthy state. If the command exit status is 1, the container is considered to be in the unhealthy state.
  • <options>: By default, the HEALTHCHECK command is invoked every 30 seconds, the command timeout is 30 seconds, and the command is retried three times before the container is declared unhealthy. Optionally, you can modify the default interval, timeout, and retries values using the following options:
  • --interval=<DURATION> [default: 30s]
  • --timeout=<DURATION> [default: 30s]
  • --retries=<N> [default: 3]

Here is an example of the HEALTHCHECK instruction:

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

If there is more than one HEALTHCHECK instruction in a Dockerfile, only the last HEALTHCHECK instruction will take effect. So you can override the health check defined in the base image. For any reason, if you choose to disable the health check defined in the base image, you could resort to the NONE option of the HEALTHCHECK instructions, as shown here:

HEALTHCHECK NONE 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset