Skip to content

Lesson 12 of 19 · 18m

Volumes: Where Container State Actually Lives

The writable layer dies with the container; named volumes, bind mounts, and tmpfs each have different persistence, ownership, and syntax traps.

A container's writable layer is temporary by design. docker rm deletes it, and it dies with the container regardless — every file your app wrote inside its own filesystem evaporates on replacement. The mental model to hold: a container is a process with a scratchpad, not a machine with a disk. Anything you would miss belongs somewhere else, and "somewhere else" has exactly three options.

The three places a write can land

  • Writable layer (overlayfs). Dies with the container. Correct for temp files and caches; wrong for anything with a heartbeat.
  • Named volume (-v pgdata:/var/lib/postgresql/data). Docker-managed storage under /var/lib/docker/volumes; survives docker rm, image rebuilds, and host reboots until docker volume rm or docker compose down -v. Critically, on first mount a named volume is initialized from the image directory's contents including ownership — which is why postgres:16 data arrives owned by uid 999 and your app-data volume arrives owned by the uid you install -d'd at build time.
  • Bind mount (-v /srv/app/data:/app/data or --mount type=bind). A literal host path. No initialization, no magic — the container sees the directory as it exists. Files created by uid 10001 inside appear on the host as uid 10001; there is no mapping without user-namespace remapping. This is the ownership trap the non-root lesson warned about, seen from the other side: chown the host dir, or match the uid.
  • tmpfs (--tmpfs /run:size=16m). RAM only, erased on stop — the right answer for pidfiles, lockfiles, and anything that must never persist or execute (noexec,nosuid).

One caveat worth internalizing: volumes have no quota. A container that fills pgdata fills the host disk the volume lives on — the resource limits from the previous module bound CPU, memory, and pids, not volume writes. Disk monitoring on the host is the missing control.

-v versus --mount

-v pgdata:/data:ro is the terse form; --mount type=volume,source=pgdata,target=/data,readonly is the explicit one. The difference that bites: -v /typo/path:/data silently creates the missing host directory, while --mount errors loudly. Under -v, typos become stray root-owned directories sitting on your host forever.

Inspecting and backing up

Volumes are just directories. docker system df -v shows which ones are bloated, and the canonical backup is a throwaway container:

docker run --rm -v pgdata:/data:ro -v "$PWD":/backup alpine \
  tar czf /backup/pgdata.tgz -C /data .

docker volume inspect pgdata tells you the mountpoint and options; docker volume prune deletes every volume no container references — check the list before running it on a host that owns anything.

Compose

Compose volumes behave the same way: pgdata:/var/lib/postgresql/data under the service plus a top-level volumes: pgdata: declaration. docker compose down keeps named volumes; down -v deletes them — the one flag that turns "reset the stack" into "lose the database." Dev workflows bind-mount source code for live reload: same mechanism, same host-side ownership story, and the reason your node_modules directory ends up root-owned on your laptop.

Try this

Run postgres:16 with a named volume, insert a row, docker rm -f the container, start a fresh one on the same volume — the row survives. Repeat without the volume and watch it vanish. Then bind-mount a host directory and inspect file ownership from both sides.

Field notes

  • SELinux hosts need :z or :Z on bind mounts or the container gets permission-denied.
  • Never run two writers on one data volume (a second postgres on the same pgdata corrupts it) — volumes are not shared-storage semantics.
  • NFS and other network volume drivers exist; latency and locking are the price you pay for them.
  • Anonymous volumes are the litter of this world: VOLUME /data in a Dockerfile or -v /data with no source leaves an unnamed volume per container. docker volume ls -f dangling=true is the list nobody reads until the disk fills.
  • Writes in the Dockerfile land under a volume mount: build into /app/data and the mount hides those bytes at runtime — named volumes copy them in once on first mount, bind mounts never.
  • docker cp is the escape hatch for one-off extraction — no backup container required.
  • Volumes never shrink; the cleanup that actually frees space is docker system prune --volumes — read the flag list twice before running it.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.