>_ IzzyLab

Hands-on task

This task is step 15 of the course “CI/CD: pipelines and docker compose”

docker-compose: healthcheck and dependency

medium~12 min

In /root/docker-compose.yml, define two services: db (postgres) with a healthcheck, and app (nginx) that starts only once db is healthy (depends_on with condition: service_healthy). The file must conform to the Compose Specification schema — run yamlschema compose /root/docker-compose.yml to see the errors.

What you'll practise:
  • docker compose
  • healthcheck
  • depends_on
  • service_healthy
  • yaml

Topic breakdown

A plain depends_on in Compose guarantees only the container start order, but doesn't wait until the service inside is actually ready to accept connections — hence the races on the first launch of the stack. To wait for real readiness, you give the service a healthcheck with a probe command (test), whose zero exit code means "healthy", plus interval and retry-count parameters. The dependent service uses the extended form of depends_on — a map where a condition of waiting for health is specified for the right service.

What's checked

  1. the file passes the Compose schema
  2. db has a healthcheck using pg_isready
  3. app waits for db to be healthy
  4. images: db is postgres, app is nginx

What it looks like