Docker_volumes
Chapter 07: Docker Volumes
Section titled “Chapter 07: Docker Volumes”Docker volumes provide persistent storage for containers. This chapter covers managing data in Docker.
Why Volumes?
Section titled “Why Volumes?”Containers are ephemeral - data is lost when they stop. Volumes persist data beyond container lifecycle.
┌─────────────────────────────────────────────────────────────────────────────┐│ Container Data Without Volumes │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Container Running Container Stopped ││ ┌──────────────────┐ ┌──────────────────┐ ││ │ Container │ │ Container │ ││ │ ┌──────────────┐ │ │ │ ││ │ │ App Data │ │ ────────▶ │ (Data Lost!) │ ││ │ │ Database │ │ │ │ ││ │ │ Logs │ │ │ │ ││ │ └──────────────┘ │ └──────────────────┘ ││ └──────────────────┘ ││ ││ Data is tied to container lifecycle ││ │└─────────────────────────────────────────────────────────────────────────────┘
┌─────────────────────────────────────────────────────────────────────────────┐│ Container Data With Volumes │├─────────────────────────────────────────────────────────────────────────────┤│ ││ Container Running Container Stopped ││ ┌──────────────────┐ ┌──────────────────┐ ││ │ Container │ │ Container │ ││ │ ┌──────────────┐ │ │ │ ││ │ │ App Data │ │◀─── Volume ───────▶│ App Data │ ││ │ └──────┬───────┘ │ │ Persists! │ ││ └───────┼─────────┘ └────────┬─────────┘ ││ │ │ ││ ┌───────▼──────────────────────────────────────────▼───────┐ ││ │ Volume │ ││ │ (/var/lib/docker/volumes/) │ ││ └─────────────────────────────────────────────────────────┘ ││ ││ Data persists beyond container lifecycle ││ │└─────────────────────────────────────────────────────────────────────────────┘Volume Types
Section titled “Volume Types”┌─────────────────────────────────────────────────────────────────────────────┐│ Docker Volume Types │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ┌────────────────┐ ┌────────────────┐ ┌────────────────┐ ││ │ Named Volumes │ │ Bind Mounts │ │ tmpfs Mount │ ││ └────────────────┘ └────────────────┘ └────────────────┐ ││ ││ Managed by Docker Host directory bind In-memory storage ││ Persist across Access host files Fast but temporary ││ container lifecycle directly ││ │└─────────────────────────────────────────────────────────────────────────────┘Named Volumes
Section titled “Named Volumes”Creating and Using Volumes
Section titled “Creating and Using Volumes”# Create a volumedocker volume create my-data
# List volumesdocker volume ls
# Inspect volumedocker volume inspect my-data
# Run container with volumedocker run -d -v my-data:/var/lib/postgresql/data postgres:15
# Remove volumedocker volume rm my-dataVolume Inspect
Section titled “Volume Inspect”# View volume detailsdocker volume inspect my-data
# Output:# [# {# "CreatedAt": "2024-01-01T10:00:00Z",# "Driver": "local",# "Labels": {},# "Mountpoint": "/var/lib/docker/volumes/my-data/_data",# "Name": "my-data",# "Options": {},# "Scope": "local"# }# ]Bind Mounts
Section titled “Bind Mounts”Mount host directories into containers:
# Mount host directorydocker run -v /host/path:/container/path nginx
# Mount current directorydocker run -v $(pwd):/workspace myapp
# Mount with read-onlydocker run -v $(pwd):/workspace:ro myappUse Cases for Bind Mounts
Section titled “Use Cases for Bind Mounts”┌─────────────────────────────────────────────────────────────────────────────┐│ Bind Mount Use Cases │├─────────────────────────────────────────────────────────────────────────────┤│ ││ 1. Development Environments ││ - Mount source code for live reload ││ - Changes reflect immediately ││ ││ 2. Configuration Files ││ - Share host config files with containers ││ - Easy to modify without rebuilding ││ ││ 3. Log Files ││ - Access container logs from host ││ - Centralized log management ││ ││ 4. Build Artifacts ││ - Share build output between containers ││ │└─────────────────────────────────────────────────────────────────────────────┘tmpfs Mounts
Section titled “tmpfs Mounts”Store data in memory (temporary):
# tmpfs mountdocker run --tmpfs /tmp:rw,size=100m myapp
# Using mount optiondocker run --mount type=tmpfs,destination=/tmp,size=100m myappVolume Drivers
Section titled “Volume Drivers”Local Driver (Default)
Section titled “Local Driver (Default)”# Default local driverdocker volume create my-volumeThird-Party Drivers
Section titled “Third-Party Drivers”- rexray: Cloud storage integration
- flocker: Cross-host container storage
- nfs: Network File System
- ceph: Distributed storage
# Using a different driver (example with NFS)docker volume create \ --driver local \ --opt type=nfs \ --opt o=addr=192.168.1.1,rw \ --opt device=:/path/to/share \ nfs-volumeVolume Management Commands
Section titled “Volume Management Commands”# Create volumedocker volume create my-volume
# List volumesdocker volume ls
# Inspect volumedocker volume inspect my-volume
# Remove volumedocker volume rm my-volume
# Remove all unused volumesdocker volume prune
# Remove unused volumes older than 24 hoursdocker volume prune --filter "until=24h"Using Volumes with Docker Compose
Section titled “Using Volumes with Docker Compose”version: '3.8'
services: db: image: postgres:15 volumes: # Named volume - db-data:/var/lib/postgresql/data
# Bind mount volumes: - ./config:/etc/postgresql
# tmpfs mount tmpfs: - /tmpfs:size=100M
app: image: myapp volumes: - app-data:/app/data - ./logs:/var/log/myapp
volumes: db-data: app-data:Sharing Data Between Containers
Section titled “Sharing Data Between Containers”Using Shared Named Volumes
Section titled “Using Shared Named Volumes”# Create shared volumedocker volume create shared-data
# Container 1 writes to volumedocker run -v shared-data:/data --name writer busybox sh -c 'echo "Hello from container 1" > /data/file.txt'
# Container 2 reads from same volumedocker run -v shared-data:/data --name reader busybox cat /data/file.txtUsing —volumes-from
Section titled “Using —volumes-from”# Create container with volumedocker create -v /data --name data-container busybox
# Use same volume in another containerdocker run --volumes-from data-container myappBest Practices
Section titled “Best Practices”┌─────────────────────────────────────────────────────────────────────────────┐│ Volume Best Practices │├─────────────────────────────────────────────────────────────────────────────┤│ ││ ✓ Use named volumes for persistent data ││ ✓ Use bind mounts for configuration ││ ✓ Use tmpfs for sensitive temporary data ││ ✓ Never store application code in volumes (use bind mounts instead) ││ ✓ Use read-only volumes when possible ││ ✓ Backup important volumes regularly ││ ✓ Use volume drivers for distributed storage ││ │└─────────────────────────────────────────────────────────────────────────────┘Backing Up Volumes
Section titled “Backing Up Volumes”# Backup volume to tardocker run --rm \ -v my-volume:/data \ -v $(pwd):/backup \ busybox \ tar cvf /backup/backup.tar /data
# Restore volume from tardocker run --rm \ -v my-volume:/data \ -v $(pwd):/backup \ busybox \ tar xvf /backup/backup.tar -C /Volume Scenarios
Section titled “Volume Scenarios”Database Data
Section titled “Database Data”# PostgreSQL with volumedocker run -d \ --name postgres-db \ -e POSTGRES_PASSWORD=secret \ -e POSTGRES_DB=mydb \ -v postgres-data:/var/lib/postgresql/data \ postgres:15Web Application Logs
Section titled “Web Application Logs”# Nginx with log volumedocker run -d \ \ -p --name nginx-web 80:80 \ -v nginx-logs:/var/log/nginx \ nginxDevelopment Environment
Section titled “Development Environment”# React app with live reloaddocker run -d \ --name react-dev \ -v $(pwd):/app \ -v /app/node_modules \ -p 3000:3000 \ -e CHOKIDAR_USEPOLLING=true \ node:18-alpine \ npm startSummary
Section titled “Summary”In this chapter, you learned:
- Why volumes are needed for data persistence
- Different volume types (named, bind, tmpfs)
- Creating and managing volumes
- Volume best practices
- Backing up and sharing volumes