Skip to content

About

Bash script that auto-updates Docker Compose projects by pulling fresh images and restarting only when something changed

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

10 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

docker-update

A Bash script that keeps Docker Compose projects up to date by pulling fresh images and restarting only the projects where something actually changed.

What it does

  • 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-update marker 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 -d for 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

Requirements

  • Bash 4+
  • Docker with the Compose v2 plugin (docker compose) or a standalone Compose v2 docker-compose binary
  • Either direct Docker access (root / docker group) or passwordless sudo — the script auto-detects which to use and never blocks on a password prompt

Usage

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 --help

The compose root can also be set with the COMPOSE_ROOT environment variable; --root takes precedence.

Pinning a project

Create an empty marker file in any project directory you never want touched:

touch /volume2/docker/myproject/.no-update

Cron example

Run every night at 03:00:

0 3 * * * /path/to/docker-update.sh >> /var/log/docker-update.log 2>&1

When output is not a terminal, colours are suppressed and each log line is prefixed with a timestamp.

How it works

  1. Takes a lock (flock) so concurrent runs exit immediately
  2. Detects how to reach Docker (docker, or sudo -n docker) and whether to use the docker compose plugin or standalone docker-compose
  3. Loops over each subdirectory in the compose root, skipping directories with a .no-update marker or no Compose file (docker-compose.yml, docker-compose.yaml, compose.yml, compose.yaml)
  4. Runs Compose from inside each project directory, so .env and docker-compose.override.yml are honoured exactly as when running Compose by hand
  5. Skips the project if no containers are running
  6. 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
  7. If an update is found: runs compose up -d --remove-orphans <running services>, which recreates only the changed containers and their dependents (including network_mode: service:<name> sidecars). When Compose supports it, --wait --wait-timeout $WAIT_TIMEOUT is 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)
  8. Verifies all previously-running services are running again; if any are missing, retries with a full compose down + compose up -d before marking the project failed
  9. Prunes dangling images (docker image prune -f) when at least one project was updated (skipped in --dry-run)
  10. Prints a summary and exits with code 1 if any project failed

Notes

  • --dry-run still pulls images (that is inherent to detecting updates); it only skips the restart and the prune.
  • WAIT_TIMEOUT (default 300) bounds the health-check wait, in seconds. --wait needs Compose v2.1+ and --wait-timeout v2.17+; the script detects what is available and warns when health checks can't be verified. SETTLE_SECONDS (default 5) is how long it pauses before re-checking which services are running.
  • The lockfile lives at ${TMPDIR:-/tmp}/docker-update.lock.

Testing

The repository ships a regression test suite that runs against a fake docker binary (tests/stub/docker) — no Docker daemon needed:

tests/run-tests.sh

It 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.

License

MIT

About

Bash script that auto-updates Docker Compose projects by pulling fresh images and restarting only when something changed

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages