Scenario
Ops ticket: We need persistent storage for a container and proof-of-backup. Create a named volume, write data into it from a container, back it up to a tar, restore into a new volume, verify the data, then clean up.
This workflow mounts host paths into containers. Double-check volume names and host paths before running tar operations so you do not overwrite or archive the wrong data.
Objective
- List existing Docker volumes (baseline evidence).
- Create a named volume called appdata.
- Write a marker file into the volume from a container.
- Read the marker file back to confirm persistence.
- Back up the volume contents to a tar archive on the host.
- Create a new volume and restore the tar into it.
- Verify restored data matches expected content.
- Remove volumes and remove the backup tar.
Concepts
- Docker volumes provide persistent storage managed by Docker and decouple data from container lifecycle.
- A temporary container can read and write volume data by mounting the volume into the container filesystem.
- Volume backup can be achieved by tarring the mounted volume contents to a host-mounted directory.
- Restore is the inverse process: mount the target volume and extract the tar into it.
- Evidence should include volume listings and content verification via a container read-back.
Walkthrough
docker volume ls
DRIVER VOLUME NAME
docker volume create appdata
appdata
docker volume ls
DRIVER VOLUME NAME
local appdata
docker run --rm -v appdata:/data alpine:latest sh -c "echo lab158-ok > /data/marker.txt"
(container exited)
docker run --rm -v appdata:/data alpine:latest cat /data/marker.txt
lab158-ok
docker run --rm -v appdata:/data -v /root:/backup alpine:latest sh -c "cd /data && tar -cf /backup/appdata-backup.tar ."
(backup created)
docker volume create appdata_restored
appdata_restored
docker run --rm -v appdata_restored:/data -v /root:/backup alpine:latest sh -c "cd /data && tar -xf /backup/appdata-backup.tar"
(restore complete)
docker run --rm -v appdata_restored:/data alpine:latest cat /data/marker.txt
lab158-ok
docker volume rm appdata appdata_restored
appdata
appdata_restored
rm -f /root/appdata-backup.tar
(removed)
Common breakpoints
If the host cannot pull images, pre-pull alpine or use an image that already exists locally.
Confirm the host path mount is correct: /root is mounted at /backup inside the container, and tar writes to /backup/appdata-backup.tar.
Ensure you extract into the mounted volume path (/data) and not somewhere else in the container filesystem.
A volume cannot be removed if a container is still using it. Confirm no containers reference appdata or appdata_restored.
Cleanup checklist
Remove the volumes and the backup tar if they still exist.
docker volume rm appdata appdata_restored 2>/dev/null || true
rm -f /root/appdata-backup.tar
docker volume ls does not show appdata or appdata_restored and /root/appdata-backup.tar is not present.
Reference
docker volume ls: List Docker volumes.docker volume create: Create a named volume.docker run --rm: Run a temporary container and remove it after exit.--rm: Automatically remove the container when it exits.-v SRC:DST: Mount a volume or host path at a container path.
tar -cf: Create a tar archive.-c: Create archive.-f: Write to file.
tar -xf: Extract a tar archive.-x: Extract archive.-f: Read from file.
docker volume rm: Remove one or more volumes.rm -f: Remove a file without prompting.-f: Force removal, no prompts.