Lesson 2: The Backup Artist (Volume Operations)
Data is only as safe as your last backup. In Docker, volumes need the same care as any database — regular backups, monitoring, and cleanup.
Backing Up a Volume
Docker doesn't have a built-in docker volume backup command. Instead, you use a helper container:
docker run --rm \
-v app-data:/data \
-v $(pwd):/backup \
alpine tar czf /backup/data-backup.tar.gz /data
What this does:
- Starts a temporary Alpine container.
- Mounts the
app-datavolume at/data. - Mounts your current directory at
/backup. - Creates a compressed archive of the volume contents.
--rmauto-removes the helper container when done.
Restoring a Volume
# Create a fresh volume
docker volume create app-data-restored
# Restore from backup
docker run --rm \
-v app-data-restored:/data \
-v $(pwd):/backup \
alpine tar xzf /backup/data-backup.tar.gz -C /
Exploring Volume Contents
Sometimes you just need to peek inside:
# List files
docker run --rm -v app-data:/data alpine ls -la /data
# Read a specific file
docker run --rm -v app-data:/data alpine cat /data/config.json
# Get a shell to browse
docker run --rm -it -v app-data:/data alpine sh
Docker Disk Usage
Docker can eat up disk space fast. Monitor it:
docker system df
TYPE TOTAL ACTIVE SIZE RECLAIMABLE
Images 15 3 4.2GB 3.1GB (73%)
Containers 5 2 150MB 120MB (80%)
Local Volumes 8 2 2.1GB 1.8GB (85%)
Build Cache - - 500MB 500MB
The Nuclear Option: Full Cleanup
# Remove stopped containers, unused images, networks, and volumes
docker system prune -a --volumes
⚠️ Warning: This removes everything not currently in use!
Development Workflow with Bind Mounts
For local development, bind mounts give you live reloading:
docker run -d \
--name dev-server \
-v $(pwd)/src:/app/src \
-v $(pwd)/package.json:/app/package.json \
-p 3000:3000 \
node:18-alpine \
sh -c "cd /app && npm install && npm run dev"
Edit files on your laptop → changes appear instantly in the container.
booting...
Mission Objective
Master volume operations:
- Backup: Run the backup command to archive your volume to
data-backup.tar.gz. - Explore: List files inside the volume with a helper container.
- Monitor: Run
docker system dfto check disk usage.