3 Hours

10. Data Persistence with Docker Volumes

Storage

Keeping Data When Containers Are Deleted

Containers are ephemeral — when you delete a container, its writable layer is destroyed along with any data written during its lifetime. For databases, user uploads, and application state, this is unacceptable. Docker volumes provide persistent storage that outlives any single container.

The Three Types of Docker Storage

Docker provides three ways to store data. Volumes are the preferred choice for production persistent data — they are managed by Docker, stored in a part of the host filesystem that is managed by Docker (`/var/lib/docker/volumes/` on Linux), and can be managed with Docker CLI commands. Bind mounts are the second type — they mount any directory from the host filesystem into the container. Bind mounts give you full control but bypass Docker's management, making them less portable. tmpfs mounts store data only in the container's memory — the data never hits the disk and is lost when the container stops. tmpfs is useful for temporary, sensitive data that should not be written to disk.

Text
┌─────────────────────────────────────────────────────────────────┐
│  Volumes (Recommended for Production) │
│  • Managed by Docker (stored in /var/lib/docker/volumes/)      │
│  • Portable across environments │
│  • Can be backed up and restored with Docker commands          │
│  • Work on Linux, Windows, and macOS │
├─────────────────────────────────────────────────────────────────┤
│  Bind Mounts │
│  • Any directory on the host (full control) │
│  • Not portable (depends on host filesystem layout)            │
│  • Used for development (live code reload) │
├─────────────────────────────────────────────────────────────────┤
│  tmpfs Mounts │
│  • Stored in memory only (never on disk) │
│  • Lost when container stops │
│  • Used for temporary, sensitive data │
└─────────────────────────────────────────────────────────────────┘

Named Volumes: The Production Standard

Named volumes are created and managed by Docker. You create a volume with `docker volume create mydata`, then reference it with `-v mydata:/app/data`. Docker ensures the volume exists (creating it if necessary) and mounts it into the container. Named volumes are portable — you can back them up, restore them, and share them across containers. Multiple containers can mount the same volume simultaneously (useful for reading shared configuration). However, be careful with multiple writers — not all applications handle concurrent writes safely. For databases like PostgreSQL, only one container should write to a volume at a time; others can read.

Bash
# Create a named volume
docker volume create postgres_data

# Inspect volume
docker volume inspect postgres_data
# Shows mountpoint on host (e.g., /var/lib/docker/volumes/postgres_data/_data)

# Use volume in container
docker run -d \
  --name postgres \
  -v postgres_data:/var/lib/postgresql/data \
  postgres:15

# Even after container deletion, volume persists
docker rm -f postgres
docker run -d --name postgres2 -v postgres_data:/var/lib/postgresql/data postgres:15
# Data is still there!

# List all volumes
docker volume ls

# Remove volume (fails if in use by any container)
docker volume rm postgres_data

# Remove all unused volumes
docker volume prune

Bind Mounts: Development and Special Cases

Bind mounts mount any directory from the host filesystem into the container. They are perfect for development because you can mount your source code directory and see changes instantly without rebuilding the image. In production, bind mounts are less common because they rely on the host filesystem layout, making them less portable. Bind mounts are also used to mount configuration files, Docker socket (`/var/run/docker.sock`), or host devices. On Windows and macOS, bind mounts go through a filesystem translation layer, which can be slower than volumes.

Bash
# Mount current directory for development (live reload)
docker run -d -p 3000:3000 -v $(pwd):/app node npm run dev

# Mount specific directory
docker run -v /home/user/data:/data alpine ls /data

# Read-only bind mount
docker run -v $(pwd):/app:ro alpine

# Mount Docker socket (for DinD — Docker in Docker)
docker run -v /var/run/docker.sock:/var/run/docker.sock docker

# In Docker Compose (development)
services:
  app:
    volumes:
      - .:/app
      - /app/node_modules  # Anonymous volume to prevent host node_modules from overriding

Backing Up and Restoring Volumes

Volumes contain critical data, so you need a backup strategy. The standard pattern is to run a temporary container that mounts the volume you want to back up and another bind mount for the backup destination, then use `tar` to archive the volume's contents. To restore, you tar extract into an empty volume. For production databases, use database-specific backup tools (like `pg_dump` for PostgreSQL) instead of filesystem-level backups to ensure consistency.

Bash
# Backup a volume to a tar file
docker run --rm \
  -v postgres_data:/source \
  -v $(pwd):/backup \
  alpine \
  tar czf /backup/postgres_backup.tar.gz -C /source .

# Restore a volume from a backup
docker run --rm \
  -v postgres_data_restored:/target \
  -v $(pwd):/backup \
  alpine \
  tar xzf /backup/postgres_backup.tar.gz -C /target

# For databases, use native backup tools
docker exec -t postgres pg_dumpall -c -U postgres > dump.sql

# Restore database
docker exec -i postgres psql -U postgres < dump.sql

Knowledge Check

Ready to test your understanding of 10. Data Persistence with Docker Volumes?

Back