Working around with Docker healthcheck logs
08 Aug 2024 - 4 min readDocker health checks are a feature that allows you to monitor the status of a container and ensure that the services running inside it are operational. A health check defines a command that the Docker engine executes inside the container at regular intervals. The result of this command is used to determine whether the container is healthy or not.
Table of Contents
- Adding a Health Check to a Docker Container
- Checking Container Health Status
- Viewing Health Check Logs
- Best Practices
- Common Use Cases for Health Checks
- Conclusion
Adding a Health Check to a Docker Container
Using Dockerfile
Here’s an example of how to add a health check in a Dockerfile
:
In this example:
--interval=30s
: The time between running the check (30 seconds).--timeout=10s
: The time after which the check is considered to have failed if no response is received (10 seconds).--retries=3
: The number of consecutive failures needed before marking the container as unhealthy.
Using docker-compose.yml
In a docker-compose.yml
file, you can add a health check like this:
Checking Container Health Status
Once you’ve set up the health check, Docker will automatically run it and update the health status of the container. You can check the status with the following command:
This will show an additional column named STATUS with health status, e.g., healthy
, unhealthy
, or starting
.
Viewing Health Check Logs
If you want to see the logs for a Docker container’s health check, you can inspect the container and look for health check details.
Here’s how you can do it:
Using docker inspect
This command will give you a JSON output containing detailed information about the health check, including the logs:
- Status: Overall health status of the container (
healthy
,unhealthy
, orstarting
). - FailingStreak: Number of consecutive failed health checks.
- Log: List of recent health check logs, including timestamps, exit codes, and command output.
Example of Interpreting Logs
Suppose you have the following output:
This tells us:
- The container is currently unhealthy.
- There have been 2 consecutive failures.
- The health check command failed to connect to
localhost:80
, indicating that the web server might not be running.
Best Practices
- Use health checks to monitor critical services within containers.
- Make health checks lightweight to avoid unnecessary overhead.
- Combine with orchestration tools like Docker Swarm or Kubernetes for automated recovery and scaling based on health status.
Common Use Cases for Health Checks
- Web Services: Ensure that a web server is responding to HTTP requests.
- Database Services: Verify database connectivity and responsiveness.
- API Endpoints: Check if an API is returning expected responses.
Web Services
Database Services
API Endpoints
Conclusion
Docker health checks are a valuable tool for ensuring that your containers and services are running smoothly. By understanding and utilizing these checks, you can increase the reliability and maintainability of your containerized applications.