Docker volume persistence: replace a container safely
Lab prerequisites
Use only an approved disposable, offline Linux VM with a local Docker Engine, ordinary local volume driver, and reviewed preloaded busybox:1.37 (record its digest, platform, exact Engine version and required utilities first). Verify the active Docker context and endpoint; every bm-v06 resource name in the lesson must be unused before starting. Never reuse a pre-existing volume. Use a dedicated lab shell, run commands individually in order, and stop on unexpected errors (the explicitly labeled negative tests are exceptions). Only delete resources you created after confirming their identity. All outputs are expected, not observed; these are manual labs, not ByteMind runnable editors. Before contacting the Engine, verify the effective local endpoint including DOCKER_HOST, DOCKER_CONTEXT and CLI overrides. Recheck the approved local image ID and native platform; if the recorded digest is unavailable, report it as unknown rather than inventing one. Use owner-set resource and time limits. If prerequisites cannot be confirmed, do the paper exercise instead. Check that the approved fixture has no declared image volumes and that the synthetic destinations and file names are unused. The bind-mount lesson alone uses a fresh host directory; do not extend it to other paths.
Scenario: a tiny inventory record
Imagine a shop that stores its inventory in a file. Application containers can be replaced when the application changes; the inventory must survive. A named volume holds the record, while each container keeps its own temporary working files. The two lifetimes are independent. This is a manual lab, not a real database or automated grader.
Write, inspect and replace
Create a fresh volume, run a writer that stores a synthetic record and a separate scratch file, then inspect its mount destination. Remove the exited writer only after verifying its identity. Run a different named reader with the same volume and verify both expectations. The persistent output should be stock=7; the scratch check should print scratch-missing.
docker volume create bm-v06-stockdocker run --pull=never --network none --name bm-v06-writer -v bm-v06-stock:/records busybox:1.37 sh -c 'printf "stock=7\n" > /records/item && printf "working\n" > /tmp/scratch'docker inspect --format '{{json .Mounts}}' bm-v06-writerdocker rm bm-v06-writerdocker run --pull=never --network none --name bm-v06-reader -v bm-v06-stock:/records:ro busybox:1.37 sh -c 'cat /records/item && if [ ! -e /tmp/scratch ]; then printf "scratch-missing\n"; else printf "unexpected-scratch\n" >&2; exit 1; fi'Check evidence and remove the test data
A record that persists on one daemon may still be lost through volume deletion, corruption or host failure. A backup needs a separate recoverable copy and a verified restoration process; live databases also require consistency-aware backups. In this lab, read the reader logs and check its exit status before removing the reader. Once you have recorded the two expected observations, discard the synthetic volume. The reader explicitly fails if the scratch path exists. Verify both expected lines and exit status 0; a missing line is not a pass.
docker logs bm-v06-readerdocker inspect --format '{{.State.ExitCode}}' bm-v06-readerdocker rm bm-v06-readerdocker volume rm bm-v06-stockManual practice
Repeat the lab with a fresh volume name and stock=12. Before executing, predict what would happen if the reader accidentally used a different volume; report expected versus actual separately.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Docker volume persistence: replace a container safely — Knowledge check
Apply the concepts from this lesson.
Which resource must stay the same across writer and reader to preserve the record?
Why is the temporary scratch file missing in the new reader?
Does surviving container replacement prove disaster recovery?