Monitor scheduled jobs in Docker and Compose

On this page

Docker runs containers; it does not schedule them. Scheduled work in a container comes from cron on the host, a cron inside the container, or an app-level scheduler, and a restart policy can quietly turn a finished job into an endless loop. Ping from the wrapper that runs the job.

How can a scheduled job fail silently on Docker and Compose?

  • The default restart policy is "no". With "always" or "unless-stopped" a job container that exits is started again straight away, so a one-shot job runs over and over instead of once on schedule.
  • "on-failure" restarts a container only when it exits with a non-zero code, and does not restart it when the Docker daemon restarts.
  • A Compose healthcheck decides whether a service container is healthy, not whether a scheduled job ran. A healthy container can sit idle for days with a stuck scheduler inside.
  • A cron daemon inside a container is easy to start without anything reading its mail or logs, so its failures go nowhere.

What are the limits of Docker and Compose?

LimitWhat the documentation says
Restart policiesno (default), on-failure[:max-retries], always, unless-stopped.
alwaysRestarts the container whenever it stops; a manually stopped container is restarted only when the daemon restarts or the container is restarted by hand.
unless-stoppedLike always, except that a container stopped by hand stays stopped, even after the daemon restarts.
Compose healthcheckDeclares a command run to decide whether the service containers are healthy, with the same defaults as the HEALTHCHECK instruction.

How do you add a CronMissed ping to Docker and Compose?

The most reliable pattern is cron or a systemd timer on the host, running a one-off container, with the ping in the wrapper around docker compose run. The container stays free of the ping URL and of any scheduler.

run-report.sh
#!/bin/sh
# /usr/local/bin/run-report.sh, started by cron: 20 4 * * * /usr/local/bin/run-report.sh
PING_URL="https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10"
curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null "$PING_URL/start"
output=$(docker compose -f /srv/app/compose.yaml run --rm report 2>&1)
status=$?
printf '%s\n' "$output" | tail -c 100000 | curl -fsS --max-time 10 --retry 5 --retry-connrefused -o /dev/null --data-binary @- "$PING_URL/$status"
exit $status

For a long-running worker that loops on its own schedule, ping from the loop after each pass.

compose.yaml
services:
  worker:
    image: example/worker:latest
    restart: unless-stopped
    environment:
      PING_URL: https://ping.cronmissed.com/5f0c1b2e-7a3d-4c8e-9b61-2d4f8a7e3c10
    command: >
      sh -c 'while true; do /app/run-once && curl -fsS --max-time 10 --retry 3 -o /dev/null "$$PING_URL"; sleep 300; done'

Which check settings fit Docker and Compose?

For the host pattern, create a cron check that matches the host's crontab. For the looping worker, use a simple check with a period slightly longer than the loop (a 300 second loop gets a 330 second period) and a grace of a few minutes. A container that is stuck, rather than stopped, is the case this catches that a restart policy cannot.

How do you confirm the check works?

  1. Run the wrapper script by hand and check the start and success pings arrive.
  2. Stop the worker container with docker compose stop worker and confirm the check goes late and then down.
  3. Make run-once fail and confirm the loop does not ping, so the check goes down after the grace.

Sources

Questions and answers

Should I use the Compose healthcheck to send pings?
It works for a long-running service, since the healthcheck command runs on an interval, but it reports that the check command ran, not that your job finished. Ping from the job itself when you can.
Can I run cron inside the container?
You can, but then the container needs the cron daemon, a way to log its output, and a restart policy that does not fight it. Host cron or a systemd timer starting a one-off container keeps the pieces simpler.
Know when this job stops

Create a check, add the ping to the job, and CronMissed alerts you the first time a run is late or reports a failure.

Start monitoring