Skip
Arish's avatar

17. Introduction to Volumes


Why Volumes?

  • Container filesystems are ephemeral
  • Volumes persist data beyond container lifecycle
  • Share data between containers

Types of Mounts

  1. Volumes - Docker-managed storage
  2. Bind Mounts - Host filesystem paths
  3. tmpfs - Memory-only storage

Named Volumes

bash
1# Create volume
2docker volume create mydata
3
4# Use volume
5docker run -v mydata:/app/data nginx
6
7# List volumes
8docker volume ls
9
10# Inspect volume
11docker volume inspect mydata
12
13# Remove volume
14docker volume rm mydata

Bind Mounts

bash
1# Mount current directory
2docker run -v $(pwd):/app myapp
3
4# Mount specific path
5docker run -v /host/path:/container/path nginx

Volume vs Bind Mount

FeatureVolumeBind Mount
Managed byDockerHost
Location/var/lib/docker/volumesAnywhere
Best forPersistenceDevelopment