1. Overview

When working with the Docker containers, ensuring the health and status of our services is crucial for a reliable and stable system.

In this tutorial, we’ll explore how to configure health checks in a Docker Compose file and demonstrate two solutions for monitoring and troubleshooting a container’s health. For simplicity, we’ll run a Nginx web service inside the Docker container.

Docker logs play a vital role in managing the containerized application, and they also help ensure the health and status of the application.

2. Initial Setup

Before we move forward and understand the different ways to view the health check, let’s first create a sample docker-compose.yml file for an Nginx service and configure health checks:

version: '3'
services:
  my-web-server:
    image: nginx:latest
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost"]
      interval: 1m30s
      timeout: 10s
      retries: 3

In the docker-compose.yml above, we have defined a service named my-web-server using the latest Nginx Docker image. Further, we configured a health check to check if the container responds at http://localhost using the curl command. Finally, the health check runs every 1 minute and 30 seconds, with a timeout of 10 seconds, and allows three retries.

3. View Health Check Logs

Let’s now explore a couple of solutions to monitor and troubleshoot the health of the my-web-server service.

3.1. Using docker ps

To quickly assess the status of the service, use the following Docker command:

$ docker ps

The above command displays a list of running containers, along with their current state. This indicates whether the my-web-server service is running or has encountered any issues. Below is a sample output of this command:

$ docker ps 

CONTAINER ID   IMAGE                       COMMAND                  CREATED        STATUS                    PORTS                   NAMES
8f4038a0c9e7   nginx:latest                "/docker-entrypoint.…"   4 mins ago   Up 4 mins (unhealthy)   80/tcp                  my-web-server

3.2. Inspecting Health Check Logs Using the docker inspect Command

In Docker, we can use the docker inspect command to get detailed information about a container, including its configuration and its current state. Moreover, we can extract specific information from the inspection results using the –format option. The jq tool can be used to process and format the JSON output.

Let’s check the overall health status in JSON format for the my-web-server service:

$ docker inspect --format "{{json .State.Health }}" my-web-server | jq

The above command provides a comprehensive JSON-formatted overview of the health status, failing streak, and an array of logs. Below is a sample health check output if the Nginx server is not running:

{
  "Status": "unhealthy",
  "FailingStreak": 7239,
  "Log": [
    {
      "Start": "2024-04-26T23:34:54.794216092Z",
      "End": "2024-04-26T23:35:03.113734054Z",
      "ExitCode": 6,
      "Output": "curl: (6) Could not resolve host: localhost\n"
    }
  ]
}

We can also use this command only to display the output logs of the health check commands. Below is an updated command:

$ docker inspect --format "{{json .State.Health }}" my-web-server | jq '.Log[].Output'
"curl: (6) Could not resolve host: localhost\n"

This command displays the output logs generated by the health checks and offers insights into any issues encountered during the health check process.

4. Conclusion

In this article, we learned to configure and view health checks in the Docker Compose application. By incorporating health checks into the Docker Compose configurations, we can proactively manage the health and status of the containers.

This approach enhances system stability, facilitates troubleshooting, and ensures the reliable operation of the Dockerized application.