Docker Tutorials

Docker Volumes & Data Persistence

24 min

Docker Volumes & Data Persistence

Docker Volumes & Data Persistence

Overview

Volumes are the recommended way to persist data generated and used by Docker containers. This guide covers volume types and best practices.


Volume Types

Named Volumes

Managed by Docker and stored on the host machine.

bash
# Create named volume
docker volume create my-data

# Use in container
docker run -v my-data:/data myapp

# List volumes
docker volume ls

# Inspect volume
docker volume inspect my-data

# Remove volume
docker volume rm my-data

Bind Mounts

Maps a host directory to a container directory.

bash
# Mount host directory
docker run -v /host/path:/container/path myapp

# Read-only mount
docker run -v /host/path:/container/path:ro myapp

tmpfs Mounts

In-memory storage for temporary data.

bash
# Mount tmpfs
docker run --tmpfs /tmp myapp

Best Practices

✅ Docker Volumes Best Practices

✅ DO Use named volumes for production data
Named volumes are managed by Docker and persist across container restarts
✅ DO Use bind mounts for development
Bind mounts enable live code updates without rebuilding
✅ DO Backup important volumes regularly
Prevent data loss with regular backups
✅ DO Use volume drivers for cloud storage
Leverage cloud storage for distributed systems
❌ DON'T Store important data in containers
Container data is ephemeral and will be lost when container is removed
❌ DON'T Use bind mounts with inconsistent permissions
Permission issues can cause access problems across different hosts

Next Steps

Press j for next, k for previous