← Back to Docker Mastery
Intermediate12 min read

Storage & Volumes

Manage persistent data in Docker — named volumes, bind mounts, tmpfs, storage drivers, and backup strategies.

Volume Types

Docker offers three mount types. Volumes are managed by Docker and stored in /var/lib/docker/volumes/. Bind mounts map a host directory into the container. Tmpfs mounts store data in memory.

Volumes are the preferred choice for persistent data — databases, uploaded files, and application state. Docker manages volume lifecycle independently of containers.

  • Volumes survive container removal — bind mounts depend on host path
  • Use :ro suffix for read-only mounts
  • Named volumes are portable across containers and hosts (with drivers)
# Named volume
docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16

# Bind mount
docker run -v $(pwd)/config:/app/config:ro myapp

# Tmpfs (in-memory)
docker run --tmpfs /tmp:rw,noexec,nosuid myapp

Volume Management

Create, inspect, and remove volumes with docker volume commands. List unused volumes with docker volume ls -f dangling=true. Prune orphaned volumes with docker volume prune.

Back up volumes by mounting them in a temporary container and archiving the contents. Restore by extracting the archive into a new volume.

# Backup a volume
docker run --rm \
  -v pgdata:/data \
  -v $(pwd):/backup \
  alpine tar czf /backup/pgdata-backup.tar.gz -C /data .

# Restore
docker run --rm \
  -v pgdata:/data \
  -v $(pwd):/backup \
  alpine tar xzf /backup/pgdata-backup.tar.gz -C /data

Bind Mounts for Development

Bind mounts map host directories into containers — ideal for development where source code changes frequently. The container reads and writes directly to the host filesystem.

Use anonymous volumes to preserve container-specific directories (like node_modules) when bind-mounting source code. This prevents host dependencies from overwriting container dependencies.

services:
  web:
    volumes:
      - ./src:/app/src          # Source code sync
      - /app/node_modules       # Preserve container deps
      - ./config:/app/config:ro # Read-only config

Storage Drivers

Storage drivers manage how image layers and container filesystems are stored. overlay2 is the default and recommended driver for modern Linux. devicemapper is legacy. btrfs and zfs offer advanced features on compatible filesystems.

For production, ensure adequate disk space for /var/lib/docker. Monitor with docker system df and set up alerts before disk fills.

docker info | grep "Storage Driver"
docker system df
docker system df -v  # Detailed breakdown

Persistent Data Patterns

Separate stateless application containers from stateful data volumes. This enables container replacement without data loss. Use init containers or entrypoint scripts to prepare volumes on first run.

For cloud deployments, use volume plugins that integrate with cloud storage (EBS, Azure Disk, GCE Persistent Disk) for volumes that survive host replacement.

# Cloud volume plugin example (AWS EBS)
docker volume create --driver rexray/ebs \
  --opt size=10 --opt volumetype=gp3 db-volume

Get In Touch


Ready to discuss your next project? Drop me a message.