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; survivesdocker rm, image rebuilds, and host reboots untildocker volume rmordocker compose down -v. Critically, on first mount a named volume is initialized from the image directory's contents including ownership — which is whypostgres:16data arrives owned by uid 999 and your app-data volume arrives owned by the uid youinstall -d'd at build time. - Bind mount (
-v /srv/app/data:/app/dataor--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
:zor:Zon bind mounts or the container gets permission-denied. - Never run two writers on one data volume (a second postgres on the same
pgdatacorrupts 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 /datain a Dockerfile or-v /datawith no source leaves an unnamed volume per container.docker volume ls -f dangling=trueis the list nobody reads until the disk fills. - Writes in the Dockerfile land under a volume mount: build into
/app/dataand the mount hides those bytes at runtime — named volumes copy them in once on first mount, bind mounts never. docker cpis 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?
Keep reading
Related articles from the SudoSecurity blog.
PostgreSQL 19 Is Out. REPACK Kept My Writes Running. The Docker Upgrade Will Stop Your App.
PostgreSQL 19 shipped on November 19 with REPACK (CONCURRENTLY), which removes table bloat without blocking writes. I measured it against VACUUM FULL, ran the upgrade from 18, and checked what breaks for self-hosters: Docker tags that refuse to start, a config setting that stops the server, and...
Self-Hosting Prometheus and Grafana: Infrastructure Monitoring That Actually Works
Build a production-grade monitoring stack with Prometheus and Grafana from scratch. Covers architecture, Docker Compose deployment, Node Exporter, alerting with Alertmanager, PromQL from basics to advanced, pre-built dashboards, and everything SudoSecurity uses to monitor its own infrastructure.
What is Kubernetes? Container Orchestration at Scale
Kubernetes runs your Docker containers across a cluster of machines, handles scheduling, health checking, rolling deployments, auto-scaling, and self-healing. When your application outgrows a single server, Kubernetes is how it runs on ten.
What is Docker? Containers, Images, and Why Every Developer Needs to Know This
Docker packages your application and everything it needs to run into a container that behaves identically on your laptop, your teammate's machine, and the production server. "It works on my machine" stops being a problem. Here is how it actually works and what production usage looks like.