A Bash script that keeps Docker Compose projects up to date by pulling fresh images and restarting only the projects where something actually changed.
- Scans every subdirectory of the compose root (default
/volume2/docker/) for a Compose file - Skips projects with no running containers, and projects containing a
.no-updatemarker file - Pulls the latest images for the services that are currently running in each project (services you stopped on purpose are left alone — never pulled, never started)
- Restarts a project only if at least one image was updated — and uses
compose up -d, so only the changed containers (and their dependents) are recreated, not the whole project - Waits for restarted services to be running and healthy (
compose up --wait, where the Compose version supports it) - Verifies that every service that was running before is running again; if not, falls back to a full
down+up -dfor that project - Prunes dangling images at the end when something was updated
- Uses a lockfile so a manual run can never collide with the cron run
- Prints a colour-coded summary of what was updated, skipped, and failed
- Bash 4+
- Docker with the Compose v2 plugin (
docker compose) or a standalone Compose v2docker-composebinary - Either direct Docker access (root /
dockergroup) or passwordlesssudo— the script auto-detects which to use and never blocks on a password prompt
chmod +x docker-update.sh # once
./docker-update.sh # normal run
./docker-update.sh --dry-run # pull and report, but restart nothing
./docker-update.sh --root /srv/dk # scan a different compose root
./docker-update.sh --helpThe compose root can also be set with the COMPOSE_ROOT environment variable; --root takes precedence.
Create an empty marker file in any project directory you never want touched:
touch /volume2/docker/myproject/.no-updateRun every night at 03:00:
0 3 * * * /path/to/docker-update.sh >> /var/log/docker-update.log 2>&1When output is not a terminal, colours are suppressed and each log line is prefixed with a timestamp.
- Takes a lock (
flock) so concurrent runs exit immediately - Detects how to reach Docker (
docker, orsudo -n docker) and whether to use thedocker composeplugin or standalonedocker-compose - Loops over each subdirectory in the compose root, skipping directories with a
.no-updatemarker or no Compose file (docker-compose.yml,docker-compose.yaml,compose.yml,compose.yaml) - Runs Compose from inside each project directory, so
.envanddocker-compose.override.ymlare honoured exactly as when running Compose by hand - Skips the project if no containers are running
- Runs
compose pull <running services>, then compares each running container's image ID against the now-current local image ID for its tag — an exact check that also catches images shared between projects, where the second project's pull is a no-op but its containers still run the old image - If an update is found: runs
compose up -d --remove-orphans <running services>, which recreates only the changed containers and their dependents (includingnetwork_mode: service:<name>sidecars). When Compose supports it,--wait --wait-timeout $WAIT_TIMEOUTis added so the command only succeeds once those services are running and, if they define a healthcheck, healthy; a service that never gets healthy marks the project failed (no down/up retry, which would only take the rest of the project offline) - Verifies all previously-running services are running again; if any are missing, retries with a full
compose down+compose up -dbefore marking the project failed - Prunes dangling images (
docker image prune -f) when at least one project was updated (skipped in--dry-run) - Prints a summary and exits with code
1if any project failed
--dry-runstill pulls images (that is inherent to detecting updates); it only skips the restart and the prune.WAIT_TIMEOUT(default300) bounds the health-check wait, in seconds.--waitneeds Compose v2.1+ and--wait-timeoutv2.17+; the script detects what is available and warns when health checks can't be verified.SETTLE_SECONDS(default5) is how long it pauses before re-checking which services are running.- The lockfile lives at
${TMPDIR:-/tmp}/docker-update.lock.
The repository ships a regression test suite that runs against a fake docker binary (tests/stub/docker) — no Docker daemon needed:
tests/run-tests.shIt covers every code path: skip reasons, update detection, restart without down, the sidecar fallback, pull/ps failures, dry-run, the lockfile, and argument handling. Note the stub mirrors the exact docker/compose commands the script uses — if you change which commands the script calls, update the stub to match. It validates the script's logic, not real Docker behaviour; --dry-run against a real host remains the integration check.
MIT