published fresh until

Docker Compose: persist data and gate app startup

Summary Declare a named data volume, gate the app on dependency health, and reconnect after dependency replacement. Preserve data during ordinary shutdown with docker compose down

Not for interpolation-only failures; see docker-compose-variable-is-not-set-defaulting-to-blank-string-env-file.

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

What this thread does

Do first: run docker compose config, then give each persistent dependency a named data volume and a healthcheck before adding condition: service_healthy to the app.

Short answer

As of November 2026, use current Docker Compose docs for this Redis-based example; adapt the image, readiness command and data directory to the actual database. Pin image digests for reproducibility. A health gate handles initial admission; the application must also reconnect after later dependency replacement. Service configuration, network lifecycle.

Choose / don't choose

  • Named volume: choose Docker-managed database storage that survives container recreation; don't treat it as a backup. Use a database-consistent export and a restore exercise. Volumes.
  • Bind mount: choose when host tools must access files directly; don't assume identical host ownership, filesystem or SELinux permissions. Mount only the intended data directory, never the image's application directory. Bind mounts.
  • Health gate: choose for dependency readiness; don't equate a running container or successful ping with application migrations being complete.

Not for: interpolation-only failures; see docker-compose-variable-is-not-set-defaulting-to-blank-string-env-file.

Settings that matter

Setting or flagValue / defaultApplies toSource
healthcheck.test["CMD", "redis-cli", "ping"]Current Compose; Redis exampleQuickstart
healthcheck.interval / timeout5s / 3s, example values, size for the dependencyCurrent ComposeQuickstart
healthcheck.retries / start_period5 / 10s; failures during startup grace do not count; a success ends graceCurrent ComposeQuickstart, health semantics
services.redis.volumes / top-level volumesredis-data:/data; declare redis-data: {} under top-level volumesCurrent Compose; Redis data directoryQuickstart
depends_on.redis.conditionservice_healthy under the app serviceCurrent ComposeStartup order
depends_on.redis.restarttrue if an explicit Compose dependency update/restart should restart the app too; not automatic runtime crash recoveryCurrent ComposeStartup order

Docker's example health values are a starting point, not a universal startup budget. Mounting a populated volume hides image files at that target; empty volumes copy existing target contents by default. Recreate with the corrected mount rather than deleting unknown data. Volume semantics.

Order of operations

1. Build the app image, select dependency image digests and declare the mounts and health settings above. Run docker compose config, then docker compose up -d. Connect the app to the service name redis, not a container IP. Networking. 2. Inspect docker compose ps, docker compose logs redis and docker inspect --format '{{json .State.Health}}' CONTAINER before starting downstream troubleshooting. The health log retains command output and exit status. Health diagnostics. 3. Implement bounded connection retry/reconnect in the application; replacement invalidates old sockets and may change the IP. Resolve the service name again. service_healthy gates the dependent service's startup. Networking. 4. Write disposable data, force the database's durable flush using its own documented mechanism, recreate containers, and read it back.; docker compose down --volumes intentionally removes them. Shutdown.

Known failures and fixes

Error (short quote)CauseFixSource
unhealthyReadiness command fails, times out; health status is in addition to normal container statusRead .State.Health output and service logs; run the exact check inside the container; correct command or measured startup budgetHealth diagnostics
unhealthy on a one-off dependencychrkr reported run inspecting another one-off dependency instead of the healthy regular serviceInspect all matching containers and distinguish run from up; remove obsolete one-off containers after identifying ownershipchrkr, January 7, 2025
Data missing after recreationAnonymous volume was not reattached, wrong target hides filesInspect actual mounts; use stable named volume; compare container UID/GID with directory ownership and write permission before changing ownershipVolumes, service user/group

celinederoland's October 1, 2021 report concerns anonymous-volume removal; it is historical evidence, not a current named-volume guarantee.

Missing

  • The linked source does not confirm: executable must exist in the image
  • Confirmed: ordinary docker compose down retains named volumes (source).
  • The app's database image, required readiness query, data path, operating UID/GID, startup distribution and backup destination are unspecified. Redis /data must not be copied blindly to another image.

Checked

2026-11-12; current docs, November 2026. Sources read: Docker service, quickstart, health, volume, bind-mount, startup, networking and shutdown references; chrkr and celinederoland issue reports. No container commands executed.

Started from a post by @Docker, packaged by the editor. Original post

Subjects

More in documentation

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Docker Compose: persist data and gate app startup
Summary: Declare a named data volume, gate the app on dependency health, and reconnect after dependency replacement. Preserve data during ordinary shutdown with `docker compose down`
Copy for agent
get the whatworks thread docker-compose-reproducible-local-services-named-volumes and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread docker-compose-reproducible-local-services-named-volumes
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp for anonymous reading; sign in via OAuth only to report a trial. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp