Skip to content

Lesson 3 of 9 · 23m

Container Filesystems, Volumes, and State That Survives

Distinguish image state, writable container state, volumes, bind mounts, and tmpfs before data loss or privilege mistakes teach you the hard way.

A container filesystem feels disposable because it should be. The image is immutable input, the container layer is ephemeral scratch space, and anything valuable needs explicit placement. Teams that blur those categories discover the difference during an upgrade, a host migration, or a rushed docker rm on the wrong Friday.

The four kinds of filesystem state

image layer       immutable content shipped with the image
container layer   writable copy-on-write state deleted with the container
named volume      lifecycle-managed persistent data owned by Docker
bind mount        host path exposed at a container path
tmpfs             memory-backed scratch data

Inspect what you actually created:

docker run -d --name pg   -e POSTGRES_PASSWORD_FILE=/run/secrets/db_password   -v pgdata:/var/lib/postgresql/data   -v /backups/postgres:/backups:ro   --tmpfs /run:rw,noexec,nosuid,size=64m   postgres:16
docker inspect pg --format '{{json .Mounts}}' | jq

The output tells you source, destination, driver, propagation, and read/write mode. Read it before assuming data is safe.

Container writable layers are not databases

Writing hot data to the container layer hurts in three ways: copy-up overhead, opaque storage growth, and accidental loss. Use a named volume for data owned by the service. Use bind mounts when the host path is the integration contract — logs collected by a host agent, source code during development, or a controlled backup directory.

docker volume create app-uploads
docker run --rm -v app-uploads:/data alpine:3.20 sh -c 'echo hello >/data/state.txt'
docker run --rm -v app-uploads:/data:ro alpine:3.20 cat /data/state.txt

Notice the second mount is read-only. Read-only is a decision you can encode at the edge, not a hope left to application discipline.

Ownership is the usual bind-mount trap

If the container process runs as UID 1000 and writes to a host directory owned by root, the write fails. If it runs as root and writes there, you now have root-owned artifacts on the host. This is not a Docker bug; it is Linux ownership doing exactly what it was designed to do.

id -u nobody 2>/dev/null || true
stat -c '%U %G %a %n' /srv/app/uploads
docker exec app id

Plan ownership deliberately. For simple single-service hosts, creating a host UID/GID pair aligned with the container user is boring and effective. In multi-tenant systems, use volume initialization jobs or rootless/user-namespaced runtimes instead of convenient root ownership.

tmpfs is a security and correctness tool

Temporary files often do not need disk persistence. tmpfs avoids copy-on-write noise and lets you enforce noexec and size limits. It is especially useful for /run, /tmp, session scratch directories, and build-time caches that must disappear.

Field notes

  • Named volumes initialized from an existing image path copy image content only when the volume is empty; later image updates do not merge into it.
  • docker compose down does not remove named volumes unless you pass -v; read the command before panic.
  • Bind mounts can shadow image content, hiding migrations or default files the image expected to exist.
  • SELinux hosts may need :z or :Z labels on bind mounts; copying those flags blindly to shared data causes real damage.
  • Backup the volume data, not the container object. Containers are runtime evidence, not durable records.

Was this lesson helpful?

Sign in to rate