Create a named Docker volume, write and verify persistent data using a container, back it up to a tar archive, restore into a new volume, validate integrity, then remove volumes and artifacts.
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.
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)
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.
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.
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.