Volumes vs. Bind Mounts

Difficulty: Intermediate

Question

Compare Docker volumes and bind mounts. When would you use each, and where does tmpfs fit in?

Answer

Volumes are storage areas created and managed by Docker (on Linux under /var/lib/docker/volumes/), referenced by name and independent of the host's directory layout. Bind mounts map an exact host path into the container, so the container sees and changes the host's files directly. Use volumes for persistent application data such as databases in production, and bind mounts for local development where you want code edits on the host to show up instantly inside the container.

Side by side

AspectNamed volumeBind mounttmpfs mount
Where data livesDocker-managed areaAny host path you chooseHost memory only
Managed withdocker volume CLINormal host toolsNothing to manage
Survives container removalYesYes (it is the host's file)No
Pre-populated from imageYes, if the volume is emptyNo, it hides the image's directoryNo
PortabilityHigh, no host path assumptionsDepends on host layout and OSHigh
Typical usePostgres data, uploadsSource code hot reload, config filesSecrets, scratch files

Behaviour differences that bite people

  • Copy-up into empty volumes: when you mount a new, empty named volume at a path where the image already has files, Docker copies those files into the volume first. A bind mount never does this; it simply hides whatever the image had at that path. This is why bind-mounting your project over /app can hide the node_modules that were installed during the build.
  • Lifecycle: removing a container does not remove a named volume. Anonymous volumes are removed only with docker rm -v or --rm runs. docker compose down -v removes the project's volumes, which can wipe a local database.
  • Permissions: bind mounts expose host UIDs and GIDs directly, so a non-root container user may get Permission denied. Volumes avoid most of this because Docker creates them.
  • Performance on Mac and Windows: Docker Desktop runs containers inside a Linux VM, so bind mounts cross a file-sharing layer and can be slower for heavy I/O. Volumes stay inside the VM and are fast. Synchronized file shares and VirtioFS have narrowed the gap, but for something like a large node_modules many teams still use a volume.

Syntax: -v versus --mount

-v name:/path and -v /host/path:/path are short and common. --mount type=volume,src=pgdata,dst=/var/lib/postgresql/data is explicit and preferred in scripts. One real difference: with -v, a missing host path is silently created as an empty directory; with --mount type=bind Docker returns an error, which catches typos.

How teams use them

A typical local setup for a Node API: bind mount ./src for hot reload, a named volume for Postgres data so it survives docker compose down, and a tmpfs mount for temporary files. In production on Kubernetes, the equivalent of a named volume is a PersistentVolumeClaim, and bind mounts (hostPath) are usually avoided for application data.

Where each mount type stores data. Volumes are owned by Docker, bind mounts by the host, tmpfs by memory.
Named volumeBind mounttmpfs
Backing storageDocker volume directoryExact host pathRAM
Best forDatabases, uploadsLive code editingSecrets, scratch data
Removed with containerNoNoYes
Hides image files at pathOnly if volume not emptyAlwaysAlways

Code examples

All three mount types with --mount

# Named volume for durable database data
docker volume create pgdata
docker run -d --name db \
  --mount type=volume,src=pgdata,dst=/var/lib/postgresql/data \
  -e POSTGRES_PASSWORD=devpass postgres:17

# Bind mount for live-editing source code (read-only inside container)
docker run -d --name api \
  --mount type=bind,src="$(pwd)/src",dst=/app/src,readonly \
  my-api:dev

# tmpfs for scratch files that must never touch disk
docker run -d --name worker --mount type=tmpfs,dst=/tmp my-worker

docker volume ls
docker volume inspect pgdata   # shows Mountpoint on the host

The --mount form makes the mount type explicit and fails loudly if a bind source path does not exist.

Compose: bind mount for code, volume for data

services:
  api:
    build: .
    volumes:
      - ./src:/app/src          # bind mount: hot reload
      - node_modules:/app/node_modules  # volume keeps container deps
  db:
    image: postgres:17
    environment:
      POSTGRES_PASSWORD: devpass
    volumes:
      - pgdata:/var/lib/postgresql/data

volumes:
  pgdata:
  node_modules:

The named node_modules volume stops the host bind mount from hiding dependencies installed in the image, and pgdata survives docker compose down unless you add -v.

Key points

  • Volumes are Docker-managed and portable; bind mounts depend on the host path.
  • An empty named volume is pre-populated from the image; a bind mount hides the image's files.
  • Removing a container keeps named volumes; docker compose down -v deletes them.
  • --mount errors on a missing bind source; -v silently creates an empty directory.
  • Use tmpfs for sensitive or throwaway data that must not be written to disk.

Common follow-up questions

  • What happens to a volume if the container using it is deleted?
  • Can multiple containers share the same volume safely?
  • How do you back up a named volume?
  • Why do bind mounts sometimes cause Permission denied errors on Linux?

Interview tips

  • Go beyond 'volumes for prod, bind mounts for dev' by explaining copy-up behaviour and lifecycle.
  • Mention the Docker Desktop performance difference; it shows hands-on experience on Mac or Windows laptops.
  • Relate volumes to Kubernetes PersistentVolumeClaims if the role touches orchestration.

Concepts covered

Named Volumes, Bind Mounts, tmpfs Mounts, --mount flag, Data Persistence

More Docker & Containers questions