Implement a Health Check in Docker Compose

Introduction

In a containerized environment, monitoring the health of your containers is essential to ensure the smooth operation of your applications. Docker Compose, a popular tool for defining and managing multi-container applications, provides a straightforward way to incorporate health checks into your container setup.

Why are Health Checks Important?

Health checks play a vital role in monitoring the status of your containers and services. They allow you to detect and respond to issues promptly, ensuring high availability and reliability. By adding health checks to your Docker Compose files, you can actively monitor the health of your containers and take appropriate actions based on the results.

The Docker Compose file with a Health Check

As an example, in this docker-compose for a Ghost blog with a MariaDB database I will add a health check. This is extremely useful because you can specify that the app in the stack will not start until the database returns a healthy status.

---
version: "3.8"

services:
  mariadb:
    image: mariadb:latest
    container_name: mariadb
    hostname: mariadb
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    environment:
      - TZ=Europe/Stockholm
      - MYSQL_DATABASE_FILE=db
      - MYSQL_USER_FILE=user
      - MYSQL_PASSWORD_FILE=password
      - MYSQL_ROOT_PASSWORD_FILE=secretpassword
    healthcheck:
      test: "healthcheck.sh --connect"
      start_period: 30s
      interval: 10s
      timeout: 10s
      retries: 5
    volumes:
      - /opt/ghost/database:/var/lib/mysql
      - /etc/localtime:/etc/localtime:ro

  ghost:
    image: ghost:latest
    container_name: ghost
    hostname: ghost
    restart: unless-stopped
    security_opt:
      - no-new-privileges:true
    environment:
      - TZ=${TZ}
      - database__client=${database__client}
      - database__connection__host=${database__connection__host}
      - database__connection__database=${database__connection__database}
      - database__connection__user=${database__connection__user}
      - database__connection__password=${database__connection__password}
      - url=${url}
    ports:
      - 2368:2368
    volumes:
      - /opt/ghost/config:/var/lib/ghost/content
      - /etc/localtime:/etc/localtime:ro
    depends_on:
      mariadb:
        condition: service_healthy

docker-compose.yml

Health Check parameters

  • test
    The 'test' parameter defines the command or script to be executed to perform the health check. It can be specified as an array of arguments or a single string. For example, 'test: ["CMD", "curl", "-f", "http://localhost/"]' performs an HTTP-based health check using cURL.
  • interval
    The 'interval' parameter specifies the time interval between consecutive health checks. It defines how frequently Docker should execute the health check. It is specified as a duration, such as '10s' for 10 seconds or '1m' for 1 minute.
  • timeout
    The 'timeout' parameter determines the maximum time Docker waits for a response from the health check command before considering it as a failure. It sets the timeout duration for each health check execution.
  • start_period
    The 'start_period' parameter defines the time duration Docker waits before starting the health checks after a container is created. This delay allows the container to initialize before health checks begin.
  • retries
    The 'retries' parameter sets the number of consecutive failures allowed before Docker considers the container as unhealthy. If the health check command fails repeatedly within the specified number of retries, the container is marked as unhealthy.
  • disable
    The 'disable' parameter, when set to 'true', disables the health check for the container. This can be useful in scenarios where a specific container does not require health checks.

These parameters provide flexibility in configuring health checks based on the specific requirements of your containers. By leveraging these parameters effectively, you can monitor and maintain the health of your containers and ensure the smooth operation of your applications.

Conclussion

Adding health checks to your Docker Compose files are crucial for ensuring the health and availability of your containerized applications. By defining health check configurations for each service, you can actively monitor the status of your containers and respond to issues promptly.