DevOps Peach

Docker & Kubernetes · Docker Storage – Volumes, Bind Mounts and Storage Drivers · Lesson 2 of 4

Three mount types — volume, bind mount, tmpfs

Volumes are managed by Docker, bind mounts point at any host directory, tmpfs lives only in RAM.

Practice interactively →

flowchart LR
  C["Container"] --> V["Volume<br/>/var/lib/docker/volumes/..."]
  C --> B["Bind mount<br/>any directory on the host"]
  C --> T["tmpfs<br/>host RAM"]
Type Where it lives Use when
Volume An area managed by Docker (/var/lib/docker/volumes/ on Linux) Persisting app data, databases — the preferred choice
Bind mount Any path on the host Mounting source code during development, host config files
tmpfs (Linux only) RAM, never written to disk Temporary or sensitive data you do not need to keep

-v and --mount

The two syntaxes are equivalent; --mount is longer but more explicit:

docker run -d --name web -v site-data:/usr/share/nginx/html nginx:1.27
docker run -d --name web --mount type=volume,src=site-data,dst=/usr/share/nginx/html nginx:1.27

docker run -d --name web -v "$(pwd)"/html:/usr/share/nginx/html:ro nginx:1.27
docker run -d --name web --mount type=bind,source="$(pwd)"/html,target=/usr/share/nginx/html,readonly nginx:1.27

docker run -d --name cache --tmpfs /tmp nginx:1.27

Common traps

Choosing between them

Need Mount type
Database files in production Named volume
Live-reload source code while developing Bind mount
Host configuration file such as nginx.conf Bind mount, read-only
Scratch space or secrets that must never touch disk tmpfs
Sharing data between containers on one host Named volume

Bind mounts depend on the host's directory layout, so they make a container less portable. Volumes work the same on every host and can use drivers for remote storage.

Platform details

tmpfs options

--tmpfs /run:rw,size=64m,mode=1777 limits the size and sets permissions. Memory used by tmpfs counts against the container's memory limit.

Secrets and config files

Swarm and Compose can deliver secrets as files under /run/secrets/<name>, mounted from memory rather than baked into the image or passed in environment variables, which are easy to leak through docker inspect or crash logs. Kubernetes uses the same idea with Secret volumes.

Seeing what is mounted

docker inspect -f '{{json .Mounts}}' web lists each mount with its type, source, destination, and whether it is read-write. Checking this is the fastest way to debug "my file is not there" problems.

Practice interactively →