Categories

Versions

Healthchecks in docker-compose

Introduction

A health check is exactly what they sound like - a way of checking the health of a resource. In the case of Docker, a health check is used to determine the health of a running container.

When a health check command is created, it defines how a container can be tested to see if it is working correctly. With no health check defined, Docker cannot know whether or not the services running within your container are actually started or not.

Sample health check within the compose file

  keycloak-db:
    image: "${REGISTRY}postgres-14:${KEYCLOAK_POSTGRES_VERSION}"
    restart: always
    hostname: keycloak-db
    environment:
      - POSTGRES_DB=${KEYCLOAK_DBSCHEMA}
      - POSTGRES_USER=${KEYCLOAK_DBUSER}
      - POSTGRES_PASSWORD=${KEYCLOAK_DBPASS}
      - POSTGRES_INITDB_ARGS=${KEYCLOAK_POSTGRES_INITDB_ARGS}
    volumes:
      - keycloak-db-vol:/var/lib/postgresql/data
    networks:
      idp-db-net:
        aliases:
          - keycloak-db
    profiles:
      - keycloak
      - deployment-init
    healthcheck:
      test: pg_isready
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 15s   
  keycloak:
    image: "${REGISTRY}rapidminer-keycloak:${KEYCLOAK_VERSION}"
    restart: always
    hostname: keycloak
    environment:
      - KC_DB=postgres
      - KC_DB_SCHEMA=public
      - KC_DB_URL_HOST=keycloak-db
      - KC_DB_URL_DATABASE=${KEYCLOAK_DBSCHEMA}
      - KC_DB_USERNAME=${KEYCLOAK_DBUSER}
      - KC_DB_PASSWORD=${KEYCLOAK_DBPASS}
      - KC_FEATURES=${KC_FEATURES}
      - KC_HOSTNAME=${PUBLIC_DOMAIN}
      - KC_HOSTNAME_PATH=/auth
      - KC_HTTP_RELATIVE_PATH=/auth
      - KC_HOSTNAME_STRICT_BACKCHANNEL=${KC_HOSTNAME_STRICT_BACKCHANNEL}
      - KC_HOSTNAME_STRICT=${KC_HOSTNAME_STRICT}
      - KC_HOSTNAME_STRICT_HTTPS=${KC_HOSTNAME_STRICT_HTTPS}
      - KEYCLOAK_ADMIN=${KEYCLOAK_USER}
      - KEYCLOAK_ADMIN_PASSWORD=${KEYCLOAK_PASSWORD}
      - KC_LOG_LEVEL=${KC_LOG_LEVEL}
      - KC_PROXY=${KC_PROXY}
      - KC_HTTP_ENABLED=${KC_HTTP_ENABLED}
    depends_on:
      proxy:
        condition: service_started
      keycloak-db:
        condition: service_healthy
    healthcheck:
      test: curl --head -fsS http://localhost:8080/auth/health/ready
      interval: 60s
      timeout: 30s
      retries: 5
      start_period: 15s
    networks:
      panopticon-net:
      platform-int-net:
        aliases:
          - keycloak
      idp-db-net:
        aliases:
          - keycloak
    profiles:
      - keycloak
      - deployment-init

The Docker Compose healthcheck contains five properties:

  • test: This property specifies the command that will be executed and is the health check of the container. This command HAS TO be available and working if everything is set up correctly.

  • interval: This property specifies the number of seconds to initially wait before executing the health check and then the frequency at which subsequent health checks will be performed.

  • timeout: This property specifies the number of seconds Docker awaits for your health check command to return an exit code before declaring it as failed.

  • retries: This property specifies the number of consecutive health check failures required to declare the container as unhealthy.

  • start_period: This property specifies the number of seconds your container needs to bootstrap. During this period, health checks with an exit code greater than zero won’t mark the container as unhealthy; however, a status code of 0 will mark the container as healthy.