Skip to main content

Docker Volumes

Docker volumes provide persistent data storage for containers, allowing data to survive container lifecycle events and be shared between containers.

Table of Contents

What are Docker Volumes?

Docker volumes are the preferred mechanism for persisting data generated by and used by Docker containers. Unlike bind mounts, volumes are completely managed by Docker and provide better portability and backup capabilities.

Key Benefits

  • Data Persistence: Data survives container removal and recreation
  • Performance: Better performance than bind mounts on Linux
  • Portability: Volumes work across different platforms
  • Backup: Easy to backup and restore
  • Sharing: Multiple containers can use the same volume
  • Management: Docker handles volume lifecycle

Volume Types

Named Volumes

The most common type, managed by Docker:

# Create a named volume
docker volume create my-data

# Use the volume in a container
docker run -v my-data:/app/data nginx

Characteristics:

  • Managed by Docker
  • Easy to identify and manage
  • Can be shared between containers
  • Survive container removal

Anonymous Volumes

Created automatically when using -v without a name:

# Creates anonymous volume
docker run -v /app/data nginx

# Creates another anonymous volume
docker run -v /app/data nginx

Characteristics:

  • Automatically named by Docker
  • Harder to manage and identify
  • Can accumulate over time
  • Good for temporary data

Bind Mounts

Direct mapping from host to container:

# Mount host directory to container
docker run -v /host/path:/container/path nginx

# Mount with specific permissions
docker run -v /host/path:/container/path:ro nginx

Characteristics:

  • Direct host access
  • Good for development
  • Platform-specific paths
  • Performance overhead

Volume Management in WhaleTUI

WhaleTUI provides comprehensive tools for managing Docker volumes through an intuitive terminal interface.

Volume List View

View all available volumes with key information:

┌─────────────────────────────────────────────────────────────────────────────┐
│ WhaleTUI - Docker Volumes │
├─────────────────────────────────────────────────────────────────────────────┘
│ DRIVER VOLUME NAME MOUNT POINT SIZE STATUS │
│ local postgres_data /var/lib/docker/volumes/... 2.1GB in use │
│ local redis_cache /var/lib/docker/volumes/... 156MB in use │
│ local app_logs /var/lib/docker/volumes/... 45MB in use │
│ local backup_storage /var/lib/docker/volumes/... 0B unused │
└─────────────────────────────────────────────────────────────────────────────┘

Volume Details

Inspect volume configuration and usage:

  • Driver: Storage driver (usually local)
  • Mount Point: Host filesystem location
  • Size: Current disk usage
  • Status: Whether volume is in use
  • Labels: Custom metadata
  • Options: Driver-specific options

Common Volume Operations

Creating Volumes

# Create basic volume
docker volume create my-volume

# Create volume with labels
docker volume create \
--label environment=production \
--label team=backend \
prod-data

# Create volume with 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

Using Volumes with Containers

# Basic volume mounting
docker run -v my-volume:/app/data nginx

# Multiple volumes
docker run \
-v postgres_data:/var/lib/postgresql/data \
-v postgres_logs:/var/log/postgresql \
postgres:15

# Read-only volume
docker run -v my-volume:/app/data:ro nginx

# Volume with specific permissions
docker run -v my-volume:/app/data:rw nginx

Inspecting Volumes

# Get volume information
docker volume inspect my-volume

# List all volumes
docker volume ls

# Filter volumes by label
docker volume ls --filter label=environment=production

Removing Volumes

# Remove specific volume
docker volume rm my-volume

# Remove all unused volumes
docker volume prune

# Force remove volume in use
docker volume rm -f my-volume

# Remove volumes by label
docker volume prune --filter label=environment=staging

Volume Configuration

Driver Options

Configure volume behavior with driver-specific options:

# Local driver with specific mount options
docker volume create \
--driver local \
--opt type=tmpfs \
--opt device=tmpfs \
--opt o=size=100m,uid=1000 \
tmp-volume

# NFS driver for remote storage
docker volume create \
--driver local \
--opt type=nfs \
--opt o=addr=192.168.1.100,rw,soft,timeo=30 \
--opt device=:/shared/data \
nfs-shared

Labels and Metadata

Organize volumes with custom labels:

# Create volume with descriptive labels
docker volume create \
--label name=database-storage \
--label environment=production \
--label team=database \
--label backup-schedule=daily \
db-storage

# Filter volumes by labels
docker volume ls --filter label=environment=production
docker volume ls --filter label=team=database

Backup and Restore

Manage volume data with backup strategies:

# Backup volume data
docker run --rm -v my-volume:/data -v $(pwd):/backup \
alpine tar czf /backup/my-volume-backup.tar.gz -C /data .

# Restore volume data
docker run --rm -v my-volume:/data -v $(pwd):/backup \
alpine tar xzf /backup/my-volume-backup.tar.gz -C /data

Best Practices

Volume Naming

  • Use Descriptive Names: postgres-data instead of vol1
  • Include Environment: prod-db-storage, dev-cache
  • Version Information: app-data-v2, backup-2024-01
  • Team Ownership: backend-logs, frontend-assets

Data Organization

  • Separate Concerns: Different volumes for different data types
  • Environment Isolation: Separate volumes for dev/staging/prod
  • Backup Strategy: Organize by backup requirements
  • Access Control: Group by access permissions

Performance Optimization

  • Use Local Storage: For performance-critical applications
  • SSD Storage: For high I/O workloads
  • Volume Pooling: Share volumes between related containers
  • Monitoring: Track volume performance metrics

Security Considerations

  • Access Control: Limit volume access to necessary containers
  • Encryption: Encrypt sensitive data volumes
  • Backup Security: Secure backup storage and access
  • Audit Logging: Track volume access and modifications

Troubleshooting

Common Issues

Volume Not Found

# Check if volume exists
docker volume ls | grep volume-name

# Inspect volume details
docker volume inspect volume-name

# Check Docker daemon logs
sudo journalctl -u docker.service

Permission Issues

# Check volume ownership
ls -la /var/lib/docker/volumes/volume-name/_data

# Fix permissions
sudo chown -R 1000:1000 /var/lib/docker/volumes/volume-name/_data

# Check container user
docker exec container-name id

Space Issues

# Check volume usage
docker system df -v

# Clean up unused volumes
docker volume prune

# Check disk space
df -h /var/lib/docker

Volume Diagnostics

# Inspect volume details
docker volume inspect volume-name

# Check volume driver status
docker info | grep -A 10 "Storage Driver"

# Monitor volume I/O
docker stats container-name

# Check volume mount points
mount | grep docker

Advanced Topics

Volume Drivers

Extend volume functionality with custom drivers:

  • Local Driver: Default local filesystem storage
  • NFS Driver: Network file system storage
  • CIFS Driver: Windows file sharing
  • GlusterFS Driver: Distributed file system
  • AWS EBS Driver: Amazon Elastic Block Store
  • Azure File Driver: Azure File Storage

Volume Plugins

Use community and commercial volume plugins:

# Install volume plugin
docker plugin install --grant-all-permissions \
store/amazon/aws-ebs-csi-driver:latest

# Create volume with plugin
docker volume create \
--driver amazon/aws-ebs-csi-driver \
--opt size=100 \
--opt type=gp3 \
ebs-volume

Volume Migration

Move volumes between systems:

# Export volume data
docker run --rm -v my-volume:/data -v $(pwd):/backup \
alpine tar czf /backup/volume-export.tar.gz -C /data .

# Transfer to new system
scp volume-export.tar.gz new-server:/tmp/

# Import on new system
docker volume create my-volume
docker run --rm -v my-volume:/data -v /tmp:/backup \
alpine tar xzf /backup/volume-export.tar.gz -C /data

Swarm Mode

In swarm mode, volumes can be shared across nodes:

# Create volume accessible to swarm services
docker volume create --driver local my-volume

# Deploy service with volume across nodes
docker service create --name app --mount type=volume,source=my-volume,target=/data myapp:latest

# Check volume usage across nodes
docker volume inspect my-volume

Key Concepts:

  • Node Accessibility: Volumes can be accessed by services running on different nodes
  • Data Persistence: Data persists across service updates and node failures
  • Storage Drivers: Different storage drivers provide various node compatibility
  • Backup Strategies: Volumes can be backed up from any node with access

Integration with WhaleTUI

WhaleTUI seamlessly integrates volume management with other Docker operations:

  • Container Management: Mount volumes when creating containers
  • Service Deployment: Configure volumes for swarm services
  • Network Configuration: Use volumes with network-isolated containers
  • Image Management: Build images with volume requirements

Keyboard Shortcuts

  • :volumes - Switch to Volumes view
  • c - Create new volume (TBD - may not be implemented yet)
  • i - Inspect volume details (TBD - may not be implemented yet)
  • r - Remove volume (TBD - may not be implemented yet)
  • Enter - View volume details

Note: Some features marked as TBD (To Be Determined) may not be fully implemented in the current version of WhaleTUI. Check the latest release notes for current feature availability.

Available Actions

Use keyboard shortcuts and navigation for quick access to:

  • Volume inspection and details
  • Viewing volume information
  • Container management (via Docker CLI)

Next Steps