Sub topic 6
Waiting for the service with health checks
This chapter assumes you can read a workflow file with services: already declared and know what a Docker HEALTHCHECK is. It covers why a started container is not always a usable one, and how to make the job wait.
Started is not ready
Docker starts a container as soon as its main process launches. A database then needs more time to initialize its storage and open its port. If your first test step connects during that gap, you get connection refused or the database system is starting up. These failures are intermittent, so they often pass on a re-run and are easy to misdiagnose.
A health check closes the gap. Docker runs a command inside the container at intervals and records the result as starting, healthy, or unhealthy. GitHub Actions starts every service container before your steps. If a service defines a health check, the runner waits for it to report healthy before the first step runs.
Adding a health check
You pass health settings through the service's options key. The runner forwards that string to docker create.
name: integration-tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
services:
postgres:
image: postgres:16
env:
POSTGRES_PASSWORD: postgres
POSTGRES_DB: app_test
ports:
- 5432:5432
options: >-
--health-cmd "pg_isready -h 127.0.0.1 -U postgres"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v4
- run: npm ci
- run: npm run test:integration
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/app_testThe >- makes YAML fold the lines into one string. Keep the quotes around the command so it stays a single argument.
| Option | Meaning |
|---|---|
| `--health-cmd` | Command run inside the container. Exit code 0 means healthy. |
| `--health-interval` | Time between checks. |
| `--health-timeout` | How long one check may run before it counts as a failure. |
| `--health-retries` | Consecutive failures allowed before the container is `unhealthy`. |
| `--health-start-period` | Grace time at startup during which failures are not counted. |
With the values above, a service that never recovers is declared unhealthy after about 50 seconds of failed checks. Size these numbers for your slowest service. Raise --health-retries or add --health-start-period 30s for databases that restore a large dump on boot.
Choosing the command
Use a probe that proves the service answers real requests, not one that only proves the process exists.
- PostgreSQL:
pg_isready - Redis:
redis-cli ping - MySQL:
mysqladmin ping -h 127.0.0.1
The -h 127.0.0.1 in the PostgreSQL command is deliberate. The official image runs a temporary server during first-time setup that listens only on a Unix socket. A socket-based pg_isready can succeed during that phase, and the real server then restarts. Checking over TCP avoids passing early.
What a health check does not cover
The check reports on the service, not on your schema. A healthy database is still empty until migrations run. Run migrations as a step after the container is healthy, as a separate step before the tests, so a failure points to the right place.
Some images ship without a suitable probe command, for example a minimal image with no shell tools. In that case, add a wait step of your own:
- name: Wait for PostgreSQL
run: |
for i in $(seq 1 30); do
if (echo > /dev/tcp/127.0.0.1/5432) 2>/dev/null; then
exit 0
fi
sleep 2
done
echo "PostgreSQL did not open port 5432" >&2
exit 1
shell: bashThis checks only that the port accepts connections. Prefer a native health check when the image allows one.
When the check fails
An unhealthy service stops the job during the "Initialize containers" phase, before your first step. Open that step's log and look for the failed container and its health output. If it fails every time, run the same --health-cmd in a local container to confirm it works. A typo in the command, a missing password variable, or a timeout shorter than the startup time are the usual causes.
Exercise
A workflow starts redis:7 as a service with no options key. Tests fail about one run in five with "connection refused" on the first test, and pass when re-run. What change fixes the cause, and why does it work?
Show answer
Add a health check to the service, for example options: >- --health-cmd "redis-cli ping" --health-interval 10s --health-timeout 5s --health-retries 5. Without it the runner has nothing to wait on, so steps start as soon as the container launches. Sometimes Redis is not yet accepting connections. With the check, the runner holds the job until the container reports healthy, so the first test always finds a live service.