Docker Volume Management

Master Docker volumes: named volumes, bind mounts, tmpfs mounts, and volume drivers. Learn when to use each type and how to manage persistent data.

Volumes

Detailed Explanation

Types of Docker Storage

Docker offers three types of mounts for persisting and sharing data.

Named Volumes (Recommended)

Docker manages the storage location. Best for databases and persistent application data:

# Create a named volume
docker volume create db-data

# Use it in a container
docker run -d -v db-data:/var/lib/postgresql/data postgres:16

# Inspect the volume
docker volume inspect db-data

Bind Mounts

Map a specific host path to a container path. Best for development (live code reloading):

# Mount current directory
docker run -d -v $(pwd)/src:/app/src my-app

# Read-only mount
docker run -d -v $(pwd)/config:/app/config:ro my-app

tmpfs Mounts

In-memory storage that is never written to disk. For sensitive data:

docker run -d --tmpfs /run --tmpfs /tmp my-app
# Or with size limit:
docker run -d --mount type=tmpfs,destination=/secrets,tmpfs-size=64m my-app

Volume Management Commands

# List all volumes
docker volume ls

# List dangling (unused) volumes
docker volume ls --filter dangling=true

# Remove a specific volume
docker volume rm db-data

# Remove all unused volumes
docker volume prune

# Backup a volume to tar
docker run --rm -v db-data:/data -v $(pwd):/backup busybox tar czf /backup/db-data.tar.gz /data

# Restore a volume from tar
docker run --rm -v db-data:/data -v $(pwd):/backup busybox tar xzf /backup/db-data.tar.gz -C /

In Docker Compose

services:
  db:
    image: postgres:16
    volumes:
      - db-data:/var/lib/postgresql/data
      - ./init.sql:/docker-entrypoint-initdb.d/init.sql:ro

volumes:
  db-data:

Use Case

Persisting database data across container restarts, sharing configuration files between containers, mounting source code for hot-reload development environments, and backing up container data.

Try It — Docker CLI Reference

Open full tool