Compose named volumes and data lifetime
A named volume is storage managed separately from the service container. Define it at the top level and attach it to the service path. Here data is mounted at /data. The fixture deliberately uses a locally built, nonroot image with a prepared /data directory and a marker file, so a new empty volume can receive the directory contents and ownership through Docker volume initialization. Verify this behavior in the approved engine before writing. A named volume normally outlives ordinary down; it is reused on the next up. A shared volume does not coordinate concurrent writers or provide a backup.
Read the configuration
services: worker: image: bm-v13-storage:lab pull_policy: never user: 10001:10001 command: - sh - -c - exec sleep 300 network_mode: none build: context: . network: none volumes: - data:/datavolumes: data: {}FROM busybox:1.37RUN mkdir -p /data && touch /data/seed && chown -R 10001:10001 /dataUSER 10001:10001CMD ["sleep", "300"]Manual lab
Manual lab only, not browser execution. Use a fresh approved disposable Linux VM with external egress blocked and a local Docker context. Check docker context show, docker version and docker compose version; verify support for each flag and field. Preload and verify the native-platform BusyBox 1.37 digest, entrypoint and required applets; the tag is mutable. Ensure bm-v13 project/image/volume names are unused and no inherited COMPOSE_PROFILES or BM13 variables interfere. Use only the synthetic files here, on a disposable copy. File-backed config/secret mounts are limited to these exact files; verify UID 10001 can read them. No public ports, external pulls/pushes, real secrets or host directory mounts. Run each command individually; stop on unexpected failures, perform scoped cleanup and dispose of the VM. Explicit negative probes are expected to fail. All results below are predictions. Before Engine requests, verify the effective endpoint including DOCKER_HOST and DOCKER_CONTEXT; a project name does not select the daemon. Review inherited COMPOSE_ENV_FILES, COMPOSE_DISABLE_ENV_FILE, profile and project settings. Use a clean shell and the exact fixture directory. Inspect only synthetic config, logs and files. If a build is required, verify the effective builder can resolve the approved local base without egress; stop if build fails, never reuse a stale tag.
Open lab-fixtures/docker-compose-configuration/04-volumes/ in the extracted archive and run from a disposable copy of that folder.
Verify a local Buildx docker driver with the reviewed base already loaded and networking blocked before building. Check the write probe before continuing. The final volume removal deletes the synthetic note permanently; skip it if identity or ownership is uncertain.
docker compose -f compose.yaml -p bm-v13-volumes config --quietdocker compose -f compose.yaml -p bm-v13-volumes build workerdocker compose -f compose.yaml -p bm-v13-volumes up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v13-volumes exec -T worker test -w /datadocker compose -f compose.yaml -p bm-v13-volumes exec -T worker sh -c 'printf "%s\n" retained-v13 > /data/note.txt'docker compose -f compose.yaml -p bm-v13-volumes downdocker compose -f compose.yaml -p bm-v13-volumes up -d --no-build --pull neverdocker compose -f compose.yaml -p bm-v13-volumes exec -T worker cat /data/note.txtThe write probe should succeed for UID 10001. After ordinary down and up, reading /data/note.txt should still return retained-v13. If ownership is unexpected, stop; do not change global permissions. Remove only the explicitly named disposable volume after checking its identity and contents. Record the project volume identity and worker ID before down; verify the same volume and a new worker ID after up. Confirm the marker and exact note before deletion. Volume initialization depends on an empty volume, the driver and mount options; it is not a general ownership repair.
Clean only this disposable project. No global prune or force removal. Verify the project is empty. Retain the base image.
docker compose -f compose.yaml -p bm-v13-volumes downdocker compose -f compose.yaml -p bm-v13-volumes ps -adocker volume inspect bm-v13-volumes_data# Only after verifying this is the disposable lab volume:docker volume rm bm-v13-volumes_datadocker image rm bm-v13-storage:labCommon mistake
Never use down -v as routine cleanup for a real application. Removing a container is different from deleting durable data.
Manual practice
Write a second synthetic note, recreate the container and predict which data survives. Explain how a backup would differ from a volume.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Compose named volumes and data lifetime — Knowledge check
Reason about the Compose configuration and its consequences.
What normally survives ordinary down?
What does the service mount path specify?
Is a shared volume a backup?