Welcome to from-docker-to-kubernetes

Volumes

Learn about Docker volumes, data persistence, and storage management

Docker Volumes

Volumes are the preferred mechanism for persisting data generated and used by Docker containers. They are completely managed by Docker and provide several advantages over bind mounts.

Container data is ephemeral by default - when a container is removed, all its data is lost. Docker volumes solve this problem by providing persistent storage that exists independently of containers. They are essential for stateful applications like databases, content management systems, and any application that needs to preserve data between container restarts.

Docker volumes are designed to be:

  • Persistent: Data survives container lifecycle
  • Portable: Can be easily moved between hosts
  • Manageable: Full lifecycle management through Docker commands
  • Performant: Optimized for I/O operations
  • Secure: Isolation from regular host filesystem paths

Types of Storage

Volumes

  • Managed by Docker
  • Stored in /var/lib/docker/volumes/
  • Best practice for persistent data
  • Can be shared across containers
  • Easy backup and migration
  • Completely isolated from host filesystem hierarchy
  • Support for volume drivers enabling cloud and remote storage
  • Efficient volume ownership and permission management
  • Pre-populated with data from container image if mount point contains data
  • Can be created independently of containers with docker volume create

Bind Mounts

  • Any location on host filesystem
  • Less functionality than volumes
  • Good for development
  • Host-dependent configuration
  • Limited portability
  • Direct access to host filesystem (potentially security risk)
  • Performance depends on host filesystem
  • Allows sharing configuration files between host and containers
  • Can override container files with host content
  • Particularly useful for development when code changes frequently

tmpfs Mounts

  • Stored in host's memory
  • Temporary storage
  • Improved performance
  • Data lost on container stop
  • Useful for sensitive information
  • Never written to host filesystem
  • Extremely fast I/O performance
  • Size limited by available host memory
  • Cannot be shared between containers
  • Good for temporary files, caches, and sensitive information like secrets ::

Volume Commands

# Create a volume
docker volume create my-volume

# Create volume with specific driver
docker volume create --driver nfs my-nfs-volume

# Create volume with labels for organization
docker volume create --label project=myapp --label environment=prod my-labeled-volume

# Create volume with specific driver options
docker volume create --driver local \
  --opt type=nfs \
  --opt o=addr=192.168.1.1,rw \
  --opt device=:/path/to/dir \
  nfs-volume

# List volumes
docker volume ls

# List volumes with filtering
docker volume ls --filter "label=project=myapp"

# List volumes with custom format
docker volume ls --format "{{.Name}}: {{.Driver}}"

# Inspect volume
docker volume inspect my-volume

# Inspect multiple volumes
docker volume inspect my-volume1 my-volume2

# Format inspect output to extract specific information
docker volume inspect --format '{{.Mountpoint}}' my-volume

# Remove volume
docker volume rm my-volume

# Force remove volume (even if in use)
docker volume rm --force my-volume

# Remove multiple volumes
docker volume rm my-volume1 my-volume2

# Remove all unused volumes
docker volume prune

# Remove volumes with confirmation disabled
docker volume prune --force

# Remove volumes with filter
docker volume prune --filter "label=environment=test"

Each command has specific use cases and can be combined with other Docker commands to create sophisticated data management workflows.

Using Volumes with Containers

Basic Volume Mount

# Create and use named volume
docker run -v my-volume:/app/data nginx

# Use shorthand syntax
docker run -v my-volume:/app/data nginx

# Use long format (more explicit)
docker run --mount source=my-volume,target=/app/data nginx

# Create anonymous volume (managed by Docker, but no user-friendly name)
docker run -v /app/data nginx

Read-Only Volume

# Mount volume as read-only with short syntax
docker run -v my-volume:/app/data:ro nginx

# Mount volume as read-only with long syntax
docker run --mount source=my-volume,target=/app/data,readonly nginx

# Mount with specific SELinux context label
docker run -v my-volume:/app/data:ro,Z nginx

Named Volume in Docker Compose

version: "3.8"
services:
  web:
    image: nginx
    volumes:
      - my-volume:/app/data
      - cache-volume:/tmp/cache
      - config-volume:/etc/nginx/conf.d:ro

  api:
    image: my-api
    volumes:
      - my-volume:/shared/data # Same volume shared with web service
      - api-logs:/var/log/api

volumes:
  my-volume:
    # Using default options
  cache-volume:
    driver_opts:
      type: tmpfs
      device: tmpfs
  config-volume:
    external: true # Use pre-existing volume
  api-logs:
    labels:
      retention: "14days"

Bind Mount Examples

# Mount current directory to container
docker run -v $(pwd):/app nginx

# Mount specific host path to container
docker run -v /host/config:/etc/nginx/conf.d:ro nginx

# Mount with SELinux context (for shared content)
docker run -v /host/data:/container/data:z nginx

tmpfs Mount Examples

# Create in-memory mount
docker run --tmpfs /app/cache nginx

# Create with size and permission options
docker run --tmpfs /app/cache:size=100M,uid=1000 nginx

# Using mount format
docker run --mount type=tmpfs,destination=/app/cache,tmpfs-size=100M nginx

Volume Use Cases

Best Practices