Volume Drivers

Difficulty: Advanced

Question

What are Docker volume drivers, and when would you use something other than the default local driver?

Answer

A volume driver decides where and how a Docker volume's data is actually stored. The built-in local driver keeps data in a directory under /var/lib/docker/volumes on the host (and can also mount NFS or CIFS shares), while third-party volume plugins let a volume live on networked or cloud storage so it can outlive the host and follow a container to another machine.

How a driver plugs into the volume lifecycle

When you run docker volume create -d <driver> data, Docker calls the driver's API (Create, Mount, Unmount, Remove, Path). On container start, Docker asks the driver to mount the volume and gets back a host path, which it then bind-mounts into the container. The container never knows whether the bytes are on a local SSD, an NFS server or a cloud block device. That separation is the whole point: application code writes to /var/lib/postgresql/data and the storage decision is made by whoever runs the platform.

The local driver does more than people think

The local driver accepts mount options that map to the Linux mount command, so for many teams it is enough:

  • --opt type=nfs --opt o=addr=10.0.1.5,rw,nfsvers=4 --opt device=:/exports/uploads mounts an NFS export.
  • --opt type=cifs mounts an SMB share (credentials go in o=).
  • --opt type=tmpfs --opt device=tmpfs --opt o=size=100m creates an in-memory volume.

The mount happens when the first container using the volume starts, so a wrong IP or a firewalled NFS port shows up as a container start error, not a volume create error.

Plugins and where they fit today

Third-party drivers are installed as managed plugins, for example docker plugin install vieux/sshfs, and then used with -d vieux/sshfs. Vendors such as Portworx and NetApp have shipped Docker volume plugins for their storage. In practice:

SituationTypical choice
Single host, database or cache datalocal named volume on fast disk
Several hosts or Swarm services sharing fileslocal with NFS options, or a vendor plugin
Kubernetes clusterNot Docker volume drivers at all; Kubernetes uses CSI drivers (EBS CSI, EFS CSI, and so on)
Throwaway scratch datatmpfs mount

The Kubernetes row matters in interviews: since Kubernetes moved to containerd and CSI, Docker volume plugins are mainly a Docker Engine and Swarm concern. Saying "on EKS we would use the EBS CSI driver with a PersistentVolumeClaim" shows you know where each abstraction lives.

Gotchas

  • Block storage is single-attach. An EBS-style volume can usually be attached to one node at a time, so two containers on different hosts cannot share it. Shared access needs a file system like NFS or EFS.
  • File locking and latency. Running PostgreSQL or SQLite on NFS invites corruption and slow fsyncs. Keep databases on local or block storage.
  • Permissions. NFS exports with root squash often break containers that run as root and expect to chown the directory.
  • Plugin availability. If a plugin is not installed or enabled on a node, containers using its volumes will not start there.
Container start with a volume driver. Docker delegates storage to the driver and only sees a host path it can bind into the container.
  1. Docker Engine → Volume driver: Mount volume 'uploads'
  2. Volume driver → Storage backend: Attach or mount NFS export
  3. Storage backend → Volume driver: Share mounted on host
  4. Volume driver → Docker Engine: Return host mount path
  5. Docker Engine → Container: Bind path at /app/uploads

Code examples

NFS-backed volume with the built-in local driver

# Create a named volume that mounts an NFS export when first used
docker volume create \
  --driver local \
  --opt type=nfs \
  --opt o=addr=10.0.1.5,rw,nfsvers=4 \
  --opt device=:/exports/uploads \
  uploads

# Any container (on any host with the same volume definition) sees the shared files
docker run -d --name web -v uploads:/app/uploads myorg/web:2.1

docker volume inspect uploads --format '{{.Driver}} {{json .Options}}'

No plugin is needed for NFS; the local driver passes these options to the kernel mount call when the volume is first mounted.

Same idea in Docker Compose

services:
  web:
    image: myorg/web:2.1
    volumes:
      - uploads:/app/uploads

volumes:
  uploads:
    driver: local
    driver_opts:
      type: nfs
      o: "addr=10.0.1.5,rw,nfsvers=4"
      device: ":/exports/uploads"

Compose creates the volume with these driver options on first up. Changing driver_opts later requires removing and recreating the volume.

Key points

  • The default local driver stores data under /var/lib/docker/volumes but can also mount NFS, CIFS and tmpfs via --opt
  • Third-party drivers are managed plugins installed with docker plugin install and selected with -d
  • Drivers let data outlive a host and be shared across hosts; the container only sees a path
  • Kubernetes does not use Docker volume drivers; it uses CSI drivers and PersistentVolumes
  • Avoid running databases on NFS-backed volumes because of locking and fsync latency

Common follow-up questions

  • What is the difference between a named volume, a bind mount and a tmpfs mount?
  • How would you share uploaded files between three replicas of a web service?
  • Why can an EBS volume not be mounted by two hosts at once?
  • How does Kubernetes handle persistent storage compared to Docker?

Interview tips

  • Start with the local driver and its NFS options before naming plugins; it shows practical experience
  • Draw the line between Docker volume drivers and Kubernetes CSI so the interviewer knows you are current
  • Call out the database-on-NFS risk unprompted; it is a classic production lesson

Concepts covered

Volume plugins, local driver, NFS mounts, docker volume create, Persistent storage, CSI

More Docker & Containers questions