Automated Docker Compose Backups with Restic: Verified Snapshot and Restore A developer published a Docker Compose stack that automates verified PostgreSQL backups using Restic, splitting the work into three short-lived stages: a Postgres 16 database with a pg_isready healthcheck, a pg_dumper job that writes a non-empty pg_dump to a shared staging volume, and a backup-verify job that initializes the Restic repository, backs up the staging volume, runs restic check --read-data, restores the latest snapshot, and asserts a marker row is present. The staging volume is mounted read-only into the verify container so the backup job cannot mutate the dump it checks, and all state lives in named volumes with no host bind mounts or Docker socket. This tutorial was written with AI assistance from the project's official documentation. Verification status is stated at the end of the article. This stack separates concerns into three short-lived, deterministic stages: db postgres:16-alpine instance with a named volume pg data . It exposes a pg isready healthcheck so downstream jobs wait for a real database, not just a listening socket. pg-dumper pg dump into a shared staging volume, and exits 0 only if the dump file is non-empty. It uses the same backup-verify restic/restic:0.17.3 container that initializes the repository if needed, backs up /staging , runs restic check --read-data , restores the latest snapshot into /tmp/restore-test , and asserts the marker text is present. It exits 0 only when the full round-trip succeeds. All persistent state lives in named volumes pg data , backup staging , restic repo . No host bind mounts, no Docker socket. The entire stack is defined in a single compose.yml plus a .env file, so it runs identically in a sandbox and on a VPS. The staging volume is mounted read-only into backup-verify , which enforces a clean boundary: the backup job cannot mutate the dump it is verifying. The Restic repository is local by default /repo , but the same container works unchanged against S3-compatible object storage by swapping RESTIC REPOSITORY . check --read-data reads the entire repository, so keep the staging volume small a few hundred MB or move the repository to object storage. RESTIC PASSWORD in .env . Losing it means losing the ability to decrypt backups. Create the project directory and write both files: mkdir -p restic-backup && cd restic-backup cat <<'EOF' compose.yml services: db: image: postgres:16-alpine environment: POSTGRES USER: ${POSTGRES USER:-app} POSTGRES PASSWORD: ${POSTGRES PASSWORD} POSTGRES DB: ${POSTGRES DB:-appdb} volumes: - pg data:/var/lib/postgresql/data healthcheck: test: "CMD-SHELL", "pg isready -U $${POSTGRES USER} -d $${POSTGRES DB}" interval: 5s timeout: 5s retries: 20 restart: unless-stopped pg-dumper: image: postgres:16-alpine depends on: db: condition: service healthy environment: PGHOST: db PGUSER: ${POSTGRES USER:-app} PGPASSWORD: ${POSTGRES PASSWORD} PGDATABASE: ${POSTGRES DB:-appdb} volumes: - backup staging:/staging configs: - source: dump script target: /usr/local/bin/dump.sh mode: 0755 entrypoint: "/bin/sh", "/usr/local/bin/dump.sh" restart: "no" backup-verify: image: restic/restic:0.17.3 depends on: pg-dumper: condition: service completed successfully environment: RESTIC REPOSITORY: ${RESTIC REPOSITORY:-/repo} RESTIC PASSWORD: ${RESTIC PASSWORD} volumes: - backup staging:/staging:ro - restic repo:/repo entrypoint: "/bin/sh", "-ec" command: - | restic snapshots /dev/null 2 &1 || restic init restic backup /staging restic check --read-data rm -rf /tmp/restore-test restic restore latest --target /tmp/restore-test test -s /tmp/restore-test/staging/appdb.sql grep -q 'restic-marker' /tmp/restore-test/staging/appdb.sql echo "backup-verify: snapshot, check, and restore OK" restart: "no" configs: dump script: content: | /bin/sh set -eu psql -v ON ERROR STOP=1 <<'SQL' CREATE TABLE IF NOT EXISTS restic marker id integer PRIMARY KEY, note text NOT NULL ; INSERT INTO restic marker id, note VALUES 1, 'restic-marker' ON CONFLICT id DO UPDATE SET note = EXCLUDED.note; SQL pg dump -Fp -f /staging/appdb.sql test -s /staging/appdb.sql echo "pg-dumper: wrote $ wc -c < /staging/appdb.sql bytes to /staging/appdb.sql" volumes: pg data: backup staging: restic repo: EOF cat <<'EOF' .env POSTGRES USER=app POSTGRES PASSWORD=replace-with-a-strong-db-password POSTGRES DB=appdb RESTIC PASSWORD=replace-with-a-strong-restic-password RESTIC REPOSITORY=/repo EOF Notes on the design: restic marker so the restore check has a stable string to grep for. The ON CONFLICT clause makes re-runs idempotent. pg dump -Fc produces a custom-format archive. It is compact and restorable with pg restore , which is what you want for real databases. restic repo volume is mounted at /repo and is the default RESTIC REPOSITORY . When you switch to S3, the volume is simply unused. Run the pipeline once and inspect exit codes: docker compose up --abort-on-container-exit --exit-code-from backup-verify You should see pg-dumper log the byte count, then backup-verify log the snapshot, check, and restore confirmation. Both one-shot services must exit 0. Inspect the repository and the restored file: docker compose run --rm backup-verify sh -ec 'restic snapshots' docker compose run --rm backup-verify sh -ec 'restic ls latest' If you want to confirm the marker survived the round-trip without re-running the full pipeline: docker compose run --rm backup-verify sh -ec 'rm -rf /tmp/restore-test && restic restore latest --target /tmp/restore-test && grep -c restic-marker /tmp/restore-test/staging/appdb.sql' Tear down without losing data: docker compose down docker compose down -v deletes pg data , backup staging , and restic repo . Use it only when you intend to start from scratch. This stack has no HTTP surface, so there is no HTTPS cutover. The production cutover is about moving the Restic repository off the VPS and scheduling the pipeline. Set the repository URL and credentials in .env . For Cloudflare R2: RESTIC REPOSITORY=s3:https://