Skillversitas

Docker Volume Drivers

Volume drivers allow the use of different storage backends for Docker volumes.

Default Volume Drivers

# View volume drivers
docker volume ls

# View volume details
docker volume inspect myvolume

NFS Volume

# Install the NFS driver
docker plugin install vieux/nfs

# Create a volume with NFS
docker volume create \
  --driver vieux/nfs \
  --opt addr=192.168.1.100 \
  --opt type=nfs \
  --opt o=addr=192.168.1.100,rw \
  --opt device=:/path/to/dir \
  nfs-volume

Cloud Volume

# AWS EBS
version: '3.8'

services:
  database:
    image: postgres:15
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:
    driver: local
    driver_opts:
      type: none
      device: /dev/xvdf
      o: bind

Use Case: Volume Drivers

# docker-compose.yml
version: '3.8'

services:
  database:
    image: postgres:15
    volumes:
      - db-data:/var/lib/postgresql/data

volumes:
  db-data:
    driver: local
    driver_opts:
      type: none
      device: /mnt/nfs/myapp-db
      o: bind

Everyday Use Case

Case: The database needs persistent storage.

# Create an NFS volume
docker volume create \
  --driver vieux/nfs \
  --opt addr=192.168.1.100 \
  --opt type=nfs \
  --opt o=addr=192.168.1.100,rw \
  --opt device=:/data/myapp \
  myapp-nfs

# Use the volume
docker run -d \
  --name db \
  -v myapp-nfs:/var/lib/postgresql/data \
  postgres:15

Best Practices

  1. Choose the right driver: Match your storage needs
  2. Back up regularly: For important data
  3. Monitor performance: Watch I/O latency
  4. Use replication: For high availability

Troubleshooting

Volume cannot be mounted

# Check the driver
docker volume inspect myvolume

# Check permissions
ls -la /mnt/nfs/myapp-db

# Check the network connection
ping 192.168.1.100

Conclusion

Volume drivers enable flexible storage backends. For applications, consider NFS or cloud storage for important data.