How to configure health checks for Docker containers?

Published on Aug. 22, 2023, 12:19 p.m.

To configure health checks for Docker containers, you can use the HEALTHCHECK instruction in your Dockerfile. This instruction lets you specify a command that will be used to check the health of your container at regular intervals. Here is an example of a Dockerfile that includes a health check:

FROM nginx

COPY index.html /usr/share/nginx/html/

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

In this example, we are using the HEALTHCHECK instruction to run a curl command that checks the presence of the index.html file every 5 minutes. If the check fails (i.e., the file is not present), the health check will report an unhealthy status and the container will be restarted.

You can also configure health checks for Docker containers using tools like Kubernetes, which allow you to define more complex health checks with additional options, like liveness probes (to determine if a container is still running) and readiness probes (to determine if a container is ready to accept requests).

Tags: