Loading...

Lab 158: Docker Volumes

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.

containers storage backup

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.

Safety note

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

Step 1 : List existing Docker volumes.
Command
docker volume ls
DRIVER    VOLUME NAME
Step 2 : Create a named volume called appdata.
Command
docker volume create appdata
appdata
Step 3 : Verify appdata exists.
Command
docker volume ls
DRIVER    VOLUME NAME
local     appdata
Step 4 : Write a marker file into the volume from a container.
Command
docker run --rm -v appdata:/data alpine:latest sh -c "echo lab158-ok > /data/marker.txt"
(container exited)
Step 5 : Verify the marker file exists by reading it from a container.
Command
docker run --rm -v appdata:/data alpine:latest cat /data/marker.txt
lab158-ok
Step 6 : Back up the volume into /root/appdata-backup.tar on the host.
Command
docker run --rm -v appdata:/data -v /root:/backup alpine:latest sh -c "cd /data && tar -cf /backup/appdata-backup.tar ."
(backup created)
Step 7 : Create a new volume called appdata_restored.
Command
docker volume create appdata_restored
appdata_restored
Step 8 : Restore /root/appdata-backup.tar into appdata_restored.
Command
docker run --rm -v appdata_restored:/data -v /root:/backup alpine:latest sh -c "cd /data && tar -xf /backup/appdata-backup.tar"
(restore complete)
Step 9 : Verify restored volume contains the marker file.
Command
docker run --rm -v appdata_restored:/data alpine:latest cat /data/marker.txt
lab158-ok
Step 10 : Remove both volumes.
Command
docker volume rm appdata appdata_restored
appdata
appdata_restored
Step 11 : Remove the backup tar from /root.
Command
rm -f /root/appdata-backup.tar
(removed)

Common breakpoints

alpine:latest missing

If the host cannot pull images, pre-pull alpine or use an image that already exists locally.

backup tar not created

Confirm the host path mount is correct: /root is mounted at /backup inside the container, and tar writes to /backup/appdata-backup.tar.

restore appears empty

Ensure you extract into the mounted volume path (/data) and not somewhere else in the container filesystem.

volume removal fails

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.

Commands
docker volume rm appdata appdata_restored 2>/dev/null || true
rm -f /root/appdata-backup.tar
Success signal

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.