Docker Volumes & Storage
Volume Drivers — Sharing Storage Across Hosts
Configuring NFS, AWS EFS, and cloud drivers for shared access.
Interview: Architectural interviews often test your knowledge of scaling storage in clusters. Understand the limits of local volumes (which lock containers to a specific host node) and explain how shared storage drivers (like NFS, AWS EFS, or third-party CSI drivers) allow container migration, state sharing, and high availability.
Introduction
Docker's default storage driver is the local driver, which writes directly to the host's local disk drive. This works perfectly when containers run on a single host. However, in cluster deployments (like Docker Swarm or Kubernetes), if a node crashes, the scheduler spins up containers on a different worker node. If the data resides only on the local disk of the crashed node, the newly scheduled container will boot up with missing files.
Why It Matters
Stateless containers can be rescheduled anywhere instantly. Stateful containers, however, need their persistent records. Using shared volume drivers ensures data availability across multiple cluster machines.
Real-World Analogy
Using a local disk is like writing your project notes in a notebook on your physical desk. If you have to move to another office building (node swap), you must bring the notebook with you, which is impossible if the office building caught fire. A shared volume driver is like writing your notes in a shared Google Doc. No matter which office desk or building you sit in, as long as you log in, you see the exact same document.
How It Works
Shared volume drivers operate by mounting network filesystems (NFS, SMB, AWS EFS) on the host system right before the container starts:
- The container scheduler launches a container requesting a shared volume.
- The local host Docker daemon invokes the specified volume driver plugin.
- The driver establishes a network handshake and mounts the remote storage system (e.g. NFS share) onto the host.
- Docker binds the host-mounted path to the container filesystem.
Internal Architecture
Shared volume drivers extend the engine API. Drivers like AWS EFS CSI, Azure Files, or GlusterFS translate API calls into network filesystem mounts. This abstracts the mounting protocol details away from the container configurations.
Visual Explanation
Practical Example
Here is how to create a volume mapping to an external NFS network share:
Common Mistakes
1. Database over NFS: NFS lacks support for high-frequency synchronous operations. Running PostgreSQL/MySQL on NFS volumes causes transaction speeds to drop dramatically due to network latency.
2. No Network Fallbacks: If the network hosting your NFS share goes offline, any container dependent on the mount will freeze or crash with I/O errors.
Quick Quiz
Q1: Why are databases usually NOT run on top of shared NFS volumes?
A) NFS does not support database engines.
B) High network latency slows down database transaction commits.
C) Databases require Windows filesystems.
Answer: B — Network-attached filesystems introduce write latency, causing performance degradation for databases.
Scenario-Based Challenge
Scenario:
Your company runs a 10-node Docker Swarm cluster hosting a Wordpress application. Wordpress requires multiple replica containers to access the same wp-content/uploads directory concurrently. Which AWS storage strategy matches this requirement?
Use **AWS EFS (Elastic File System)** with a shared volume driver. AWS EBS (Elastic Block Store) only supports mounting to a single instance at a time (ReadWriteOnce), whereas AWS EFS supports concurrent mounts (ReadWriteMany), allowing all Wordpress containers across all nodes to read and write to the uploads folder simultaneously.
Debugging Exercise
Failure Case:
A container hangs during startup, throwing: error mounting volume: connection timed out.
How would you debug this?
View Solution
A connection timeout indicates network port blocking.
Verification: Check if the network path is open. For NFS, check if host can ping target IP and if port 2049 is open. Verify Security Groups (AWS) or firewall rules allow the container host to communicate with the storage host.
Interview Questions
1. Explain the difference between EBS (Block Storage) and EFS (File Storage) in container deployments.
EBS provides high performance block volumes that can mount to only one node at a time (RWO). EFS provides network-attached filesystem capabilities allowing dozens of nodes to read and write concurrently (RWX).
2. What is the role of a CSI (Container Storage Interface) driver?
CSI is a standardized API that allows storage providers to write drivers that integrate seamlessly with container orchestrators (like Kubernetes) without modifying the core orchestrator codebase.
Production Considerations
Always configure storage quotas to prevent any single container from filling up shared network storage and impacting other services. Enable encryption-in-transit when mounting remote storage.