قالب وردپرس درنا توس
Home / Tips and Tricks / How (and Why) to Add Health Checks to Your Docker Containers – CloudSavvy IT

How (and Why) to Add Health Checks to Your Docker Containers – CloudSavvy IT



Blue Docker logo on a purple background

You’ve built your Docker image, pushed it to your registry, and started a new container in production. Everything works while you go home, but you get error messages when you come back the next morning. Your container will still run, but no requests will be displayed.

This scenario may be uncomfortably familiar to operations teams working with Docker. Here̵

7;s how to use Docker’s health monitoring feature to get accurate data about the availability of your services.

How health checks work

Health checks allow a container to expose the availability of its workload. This is independent of whether the container is run. If your database goes down, your API server will not be able to process requests, even though the Docker container is still running.

This makes for useless experiences while troubleshooting. A simple docker ps would report the container as available. Adding a health check extends the docker ps output to include the true state of the container.

You configure container health checks in your Dockerfile. This accepts a command that the Docker daemon runs every 30 seconds. Docker uses the command’s exit code to determine the health of your container:

  • 0 – The container is healthy and working normally.
  • 1 – The container is unhealthy; the workload may not work.
  • 2 – This status code is reserved by Docker and should not be used.

When HEALTHCHECK is present in a Dockerfile, you will see the health of the container in the STATUS column when you run docker ps.

Health is not immediately checked when creating containers. The status is displayed as: starting before the first check runs. This gives the container time to run startup tasks. A container with a successful health check is displayed as: healthy; an unhealthy container is displayed unhealthy.

Writing health check assignments

Container health checks are configured with the HEALTHCHECK instruction in your Dockerfile. You must use a health check command that is appropriate for your container. For web servers, curl gives you an easy way to run a simple readiness check. Ping a known endpoint to be available when your service is available.

FROM nginx:latest
HEALTHCHECK CMD curl --fail http://localhost/api/healthcheck || exit 1

This example would mark the container as unhealthy if your server server /api/healthcheck endpoint has issued an error state.

You can use docker inspect to see the output of status check commands. This is useful when debugging your HEALTHCHECK instruction.

docker inspect --format="{{json .State.Health}}" my-container

To replace my-container with the ID or name of the container you want to inspect. These details are shown in docker ps output.

Customize health checks

Docker allows you to adjust the frequency of health checks. You can also change the criteria that mark a container as unhealthy.

Four options are available:

  • --interval – Set the time between health checks. Allows you to override the default value of 30 seconds. Use a higher interval to increase the time between checks. This is useful if you have a low priority service where regular health checks can affect performance. Use a more regular frequency for critical services.
  • --start-period – Set the duration after a container is started when the health checks should be ignored. The command will still be executed, but a bad status will not be counted. This gives containers time to complete startup procedures.
  • --retries – This allows you to require multiple consecutive errors before a container is marked as unhealthy. It defaults to 3. If a health check fails, but the next one succeeds, the container does not transfer to unhealthy. It becomes unhealthy after three consecutive failed checks.
  • --timeout – Set the timeout for status check commands. Docker treats the check as failed if the command does not exit within this time frame.

Options are passed as flags to the HEALTHCHECK instruction. Provide them before the health check command:

HEALTHCHECK --interval=60s --retries=5 CMD curl --fail http://localhost || exit 1

This configuration instructs Docker to run curl every 60 seconds. The container is marked as unhealthy if five consecutive tests have a non-zero exit code.

Job format

The HEALTHCHECK command syntax supports either a flat CMD or an entry point style exec series. You can also pass NONE instead of CMD disable status checks:

HEALTHCHECK NONE

Allows you to block the health checks of your base image. Each HEALTHCHECK instruction will overwrite any previous instructions in the layers of your image.

What about Docker Compose?

Docker Compose also supports health check definitions. add a healthcheck section at your service:.

version: "3"
services:
  app:
    image: nginx:latest
    ports:
      - 80:80
    healthcheck:
      test: curl --fail http://localhost || exit 1
      interval: 10s
      retries: 5
      start_period: 5s
      timeout: 10s

The test key defines the command to run. The other keys refer to the parameters accepted by the Docker file HEALTHCHECK instruction.

Resume

A . Set up HEALTHCHECK instruction makes it easier to diagnose a misbehaving container. You can track the readiness of your workload regardless of the container’s ‘running’ status.

Health checks are compatible with any command that a 0 or 1 exit code. You can use common commands such as: curl and ping to inspect web services and databases. For more advanced control, write a special script and include it in your images.


Source link