Stage 4 · Provision
Networking, Storage & Compose Deep Dive
Volume Lifecycle
Manage named volumes, bind mounts, tmpfs mounts, ownership, backups, and docker volume prune risks.
Volume Types
Docker supports three types of storage: named volumes (Docker-managed), bind mounts (host path), and tmpfs (memory). Each has different lifecycle, performance, and backup characteristics. Understanding these differences prevents data loss.
| Type | Managed By | Persistence | Performance |
|---|---|---|---|
| Named volume | Docker | Survives container removal | Good |
| Bind mount | User | Survives container removal | Best (native FS) |
| tmpfs | Kernel | Lost on container stop | Best (RAM) |
Named Volume Lifecycle
# Create a volume
docker volume create mydata
# Use the volume
docker run -d -v mydata:/var/lib/postgresql/data postgres
# List volumes
docker volume ls
# Inspect volume details
docker volume inspect mydata
# Shows: Mountpoint, Labels, Scope, Driver
# Remove a specific volume
docker volume rm mydata
# Remove all unused volumes
docker volume prune
# Remove volumes with docker compose
docker compose down -vNamed volumes persist after container removal. Docker manages the storage location. The volume exists until explicitly deleted with docker volume rm or docker volume prune.
Bind Mount Lifecycle
# Bind mount a host directory
docker run -v /host/path:/container/path myapp
# Read-only bind mount
docker run -v /host/path:/container/path:ro myapp
# Bind mount with ownership remapping
docker run -v /host/path:/container/path:U myapp
# Verify bind mount
docker inspect --format '{{json .Mounts}}' mycontainer | jq
# Cleanup — bind mounts are not cleaned by docker
rm -rf /host/pathBind mounts reference host paths directly. Docker does not manage their lifecycle. You must clean up bind mount data manually. The :U flag remaps ownership to match the container user.
tmpfs Lifecycle
# Mount tmpfs with size limit
docker run --tmpfs /tmp:rw,size=100m,noexec myapp
# Mount tmpfs for secrets
docker run --tmpfs /run/secrets:rw,noexec,nosuid myapp
# Verify tmpfs mount
docker exec myapp df -h /tmp
# Filesystem Size Used Available Use% Mounted on
# tmpfs 100M 0 100M 0% /tmp
# tmpfs is lost when container stops
docker stop mycontainer
docker start mycontainer
# /tmp is empty againtmpfs mounts use RAM. They are fast but limited by available memory. Data is lost when the container stops. Use them for temporary files, secrets, or any data that should not persist.
Ownership Management
# Problem: files owned by root inside container
docker run -v mydata:/data alpine sh -c "touch /data/test && ls -la /data"
# -rw-r--r-- 1 root root 0 Jan 15 12:00 test
# Solution 1: Use :U flag
docker run -v mydata:/data:U alpine sh -c "ls -la /data"
# Files owned by container user
# Solution 2: Match user IDs
docker run --user $(id -u):$(id -g) -v mydata:/data alpine
# Solution 3: Fix in Dockerfile
RUN chown -R appuser:appuser /data
# Solution 4: Init container for permission setup
docker run -v mydata:/data alpine chown -R 1000:1000 /dataOwnership mismatches between host and container cause permission errors. The :U flag is the simplest fix. For production, set ownership in your Dockerfile.
Backup and Prune
# Backup a named volume
docker run --rm -v mydata:/source -v $(pwd):/backup alpine \
tar czf /backup/mydata-backup.tar.gz -C /source .
# Restore from backup
docker run --rm -v mydata:/target -v $(pwd):/backup alpine \
tar xzf /backup/mydata-backup.tar.gz -C /target
# List unused volumes
docker volume ls -f dangling=true
# Prune unused volumes (DESTRUCTIVE)
docker volume prune
# Prune with filter
docker volume prune --filter label=test
# Check what will be removed
docker volume ls -f dangling=trueAlways backup named volumes before pruning. docker volume prune removes volumes not referenced by any container. There is no undo — data is permanently deleted.
docker volume prune removes all dangling volumes. This includes data from stopped containers that you might want to keep. Always verify with docker volume ls -f dangling=true before pruning.
Mark this lesson complete to store local progress and unlock a cleaner resume path the next time you visit.