Docker HEALTHCHECK: what it does and how to ping CronMissed
On this page
The Docker HEALTHCHECK instruction runs a command inside a container on a timer and records whether the container is healthy. It checks the container; it does not run a job or follow a schedule. A scheduled container job reports to CronMissed with a ping after it finishes, and a long-running container can ping from its health check.
What does the HEALTHCHECK instruction do?
HEALTHCHECK [OPTIONS] CMD command runs the command inside the container. Exit code 0 means healthy and exit code 1 means unhealthy. A container starts as starting, becomes healthy when a check passes, and becomes unhealthy when the check fails retries times in a row. HEALTHCHECK NONE turns off a check inherited from the base image, and if a Dockerfile lists several, only the last HEALTHCHECK instruction counts.
FROM alpine:3.20
RUN apk add --no-cache curl
COPY server /usr/local/bin/server
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=5s --start-period=20s --retries=3 \
CMD curl -fsS --max-time 4 http://localhost:8080/health || exit 1
CMD ["/usr/local/bin/server"]The || exit 1 turns every curl failure into exit code 1, the code Docker documents for unhealthy. Compose takes the same settings under healthcheck:, and a service can wait for a healthy one:
services:
api:
image: example/api:1.4
healthcheck:
test: ["CMD", "curl", "-fsS", "--max-time", "4", "http://localhost:8080/health"]
interval: 30s
timeout: 5s
retries: 3
start_period: 20s
worker:
image: example/worker:1.4
depends_on:
api:
condition: service_healthyWhich options and exit codes does it use?
| Option | Default | What it sets |
|---|---|---|
--interval | 30s | The wait between checks. Without a start period, the first runs one interval after the container starts. |
--timeout | 30s | How long one check may run. A check that runs longer counts as failed and is killed. |
--start-period | 0s | A grace for slow starts. Failures in it do not count toward the retries, but once a check passes, later failures count. |
--start-interval | 5s | The wait between checks during the start period, until one passes. Needs Docker Engine 25.0 or later. |
--retries=N | 3 | How many failures in a row make the container unhealthy. |
- Exit code 0: healthy. Exit code 1: unhealthy. Exit code 2 is reserved by Docker, so do not use it.
- Output on stdout and stderr is stored with the health status; only the first 4096 bytes are kept. Read it with
docker inspect --format '{{json .State.Health}}' <container>. docker psshows the status in parentheses after the uptime, and Docker raises ahealth_statusevent each time it changes.
What does HEALTHCHECK not do?
- It does not run a job. Nothing starts at a time of day because a health check exists.
- It does not schedule anything. The interval is a gap between checks, not a clock time, and it is not a cron expression.
- It does not restart a container. A restart policy reacts when a container exits, not when it turns unhealthy.
- It does not tell you. The status sits on the host until something reads it, and a container that was never started has no status at all.
That last gap is where CronMissed fits. A job that did not run, or a host that is down, produces no health result, only silence.
How does a scheduled container job ping CronMissed?
Let cron, a systemd timer or a Kubernetes CronJob start a container that does one job and exits, and ping from the command that started it. docker run exits with the container's own exit code, so && pings only when the job succeeded:
# Host crontab: start the job container, ping only if it exits 0
15 3 * * * docker run --rm example/report /app/nightly-report && curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10
# Or report the exit status, so a failure alerts at once
15 3 * * * docker run --rm example/report /app/nightly-report; curl -fsS --max-time 10 --retry 5 -o /dev/null https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10/$?To keep the ping inside the image instead, use the entrypoint wrapper in the Docker snippet. It sends the start, the exit status and the end of the output. For Compose and Kubernetes, see the Docker and Compose guide and the Kubernetes CronJob guide.
How can a container ping CronMissed from its health check?
A long-running container can report that it is alive: the health check pings CronMissed only after the app itself checks out. If the container stops, hangs or turns unhealthy, the pings stop, and CronMissed alerts you even when the whole host is gone.
#!/bin/sh
# Exit 1 when the app is unhealthy. Ping only when it is healthy.
curl -fsS --max-time 5 -o /dev/null http://localhost:8080/health || exit 1
if [ -n "$CRONMISSED_PING_URL" ]; then
curl -fsS --max-time 5 -o /dev/null "$CRONMISSED_PING_URL" || true
fiCOPY healthcheck.sh /healthcheck.sh
RUN chmod +x /healthcheck.sh
HEALTHCHECK --interval=60s --timeout=15s --start-period=30s --retries=3 CMD /healthcheck.shdocker run -d --env CRONMISSED_PING_URL="https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10" example/api:1.4- Make a check with a simple schedule of 2 minutes, twice the interval, and a grace time of a few minutes.
- Keep the interval at 60 seconds or more. A check records at most five pings a minute, and the extra ones are answered but not recorded.
- The ping failing never fails the health check, because of
|| true. The container stays healthy and CronMissed raises the alert.
Sources
- Dockerfile reference: HEALTHCHECK , read 3 November 2026
- Compose file reference: services healthcheck and depends_on , read 3 November 2026
- Docker Docs: start containers automatically , read 3 November 2026
Questions and answers
- Every container starts as starting. It stays there until a check passes, or fails enough times in a row to make it unhealthy. Without a start period, the first check runs one interval after the container starts, so a long interval holds the status there for that long.
- Not in plain Docker. Restart policies act when a container exits. Something else has to read the health status and act on it.
- No. It runs a command to check the container, on an interval that starts from the container's start. Use cron or a timer for work that must happen at set times.
Why is my container stuck in starting?
Does an unhealthy container restart on its own?
Can the health check replace a cron job?
Create a check, add its ping URL to the job, and CronMissed alerts you the first time a run is late or reports a failure.
Start monitoring