Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

Docker Volumes vs Bind Mounts: Data Persistence, tmpfs, and Permissions

Which storage survives what, why bind-mount permissions fail with the wrong uid, and the helper-container pattern for backing up a volume.

~5 min readDocker
#Docker#Storage#DevOps
3D illustration contrasting managed Docker volume storage vaults with host filesystem bind mount pipelines

Quick Summary

A container's writable layer dies with the container. Anything that must survive belongs outside it: a volume (managed by Docker, stored under /var/lib/docker/volumes), a bind mount (a host path mounted directly), or tmpfs (memory only, gone on stop). Choose by ownership: volumes for data the application manages and Docker should back up, bind mounts for source code and config you edit on the host, tmpfs for scratch files and secrets you never want on disk.

The differences that bite are permissions and lifecycle. A bind mount carries the host's ownership into the container, so a process running as uid 1000 writing into a directory owned by root fails with EACCES. A volume is created empty and initialised from the image's directory contents, and deleting the container does not delete it — until someone runs docker compose down -v.

Basic syntax and query signatures

bash
docker run -d --name db -v pgdata:/var/lib/postgresql/data postgres:17 # named volume docker run --rm -v "$PWD/src:/app/src:ro" node:22 npm test # bind mount, read-only docker run --rm --tmpfs /tmp:size=64m,mode=1777 alpine sh # memory-backed
yaml
services: db: volumes: - pgdata:/var/lib/postgresql/data api: volumes: - ./src:/app/src:ro - /app/node_modules # anonymous volume, masks the bind mount - type: tmpfs target: /app/tmp tmpfs: size: 65536000 volumes: pgdata:

-v and --mount express the same thing with different defaults. -v creates a missing host directory automatically (as root), while --mount type=bind fails if the source does not exist — which is the behaviour you want in production, because a typo becomes an error instead of an empty directory.

Key comparison table

StorageLives onSurvives container removalSurvives down -vPerformanceManaged by
Named volume/var/lib/docker/volumesYesNoNativeDocker
Anonymous volumeSame, with a hash nameNot after --rm, orphaned otherwiseNoNativeDocker
Bind mountHost pathYesYesHost filesystem speedYou
tmpfsRAMNon/aFastestKernel
Container layerOverlay filesystemNon/aSlowest on writesContainer
Use caseBest choiceWhy
Database dataNamed volumeDocker manages it; consistent across hosts
Source code in developmentBind mountEdits visible instantly
node_modules in developmentAnonymous volume over the bindKeeps host and container binaries separate
Secrets at runtimetmpfsNever touches disk
Config filesBind mount, :roEditable without rebuilding
Build cacheBuildKit cache mountNot part of the image

Practical edge cases and deep dives

1. Permission mismatch on bind mounts

bash
# Host file owned by uid 1000; container process runs as uid 1000 — works docker run --rm -v "$PWD/data:/data" alpine sh -c 'echo hi > /data/x' # Container runs as root; the file lands on the host owned by root docker run --rm -v "$PWD/data:/data" alpine sh -c 'echo hi > /data/rootfile'

The container's uid is a number on the host kernel, not a name. If the image runs as node (uid 1000) and the mounted host directory is owned by root with mode 755, the write fails. Three ways out, in order of preference:

yaml
services: api: user: "${UID:-1000}:${GID:-1000}" # run as the host user volumes: - ./data:/data
bash
# or: make the directory writable by the container's uid sudo chown -R 1000:1000 ./data # or: chmod 0777 in a dev-only setup

The third option is a named volume, where Docker copies the image's directory ownership into the volume on first use and the mismatch disappears. The general rule: bind mounts are for files you own; volumes are for files the container owns.

On SELinux-enabled hosts (Fedora, RHEL), add the label flag: -v "$PWD/data:/data:z" relabels for shared access, :Z for exclusive. Without it the container gets Permission denied even with correct Unix permissions.

2. Volumes are initialised once, then never again

bash
docker volume create appdata docker run --rm -v appdata:/var/lib/postgresql/data postgres:17 # dir populated from image # next run: the volume already has content, image contents are NOT copied again

Docker copies the image's contents into an empty named volume on first mount. That is why a database volume created against one image version keeps working after an upgrade — and why an entrypoint script that generates config into /data on first boot does not regenerate it later, because the file already exists. The behaviour is also why an anonymous volume over /app/node_modules works: the image's modules are copied in before the bind mount hides the directory.

Anonymous volumes accumulate. docker run with a bare container path creates one per run; removing the container without -v orphans it, and docker volume ls -qf dangling=true grows until someone prunes. Declaring the volume explicitly in Compose gives it a name you can find, back up, and delete on purpose.

3. tmpfs for secrets and scratch space

bash
docker run --rm \ --mount type=tmpfs,destination=/run/secrets,tmpfs-size=1048576 \ --env-file /dev/null \ myapp

A tmpfs mount is RAM-backed: nothing is written to the host disk, and everything disappears when the container stops. That makes it right for tokens written at startup and for a build's temporary files, and wrong for anything that must outlive the container — including anything the process expects to read after a restart. Size it explicitly (tmpfs-size); the default is half of host RAM, which one runaway log file can consume.

Secrets that only need to exist for a single command belong in a BuildKit secret mount during build, or an orchestrator's secret store at runtime, rather than in an environment variable — environment variables are visible in docker inspect and in /proc/<pid>/environ.

4. Bind mount performance is a host-OS question

On Linux, a bind mount is essentially the same filesystem, so the overhead is negligible. On macOS and Windows, the filesystem is virtualised (virtiofs or gRPC-FUSE on Docker Desktop), and thousands of small-file operations — exactly what npm install and file watchers do — become 10–50× slower. The standard mitigation is the anonymous volume over node_modules shown above: dependency files live in the VM's filesystem, source files stay bind-mounted.

yaml
services: web: volumes: - .:/app - /app/node_modules # keep deps inside the VM

The same shape applies to .next, target, and __pycache__. An alternative is :cached/:delegated consistency flags, but they are legacy on modern Docker Desktop and hide rather than fix the difference.

5. Backing up and restoring a volume

bash
# Archive docker run --rm -v pgdata:/data:ro -v "$PWD/backup:/backup" alpine \ tar czf /backup/pgdata-$(date +%F).tar.gz -C /data . # Restore into a fresh volume docker run --rm -v pgdata-new:/data -v "$PWD/backup:/backup" alpine \ sh -c 'tar xzf /backup/pgdata-2026-11-13.tar.gz -C /data'

docker cp cannot reach a volume directly, so the helper-container pattern is the portable way. For a database, stop the container (or use pg_dump) first — tarring a live Postgres data directory produces a corrupt backup that only fails at restore time, which is the worst moment to find out. docker volume inspect pgdata gives the host path, and copying from there works on a Linux host but not on Docker Desktop, where the volume lives inside the VM.

Common mistakes and runtime errors

Error or symptomCauseFix
Permission denied writing to a bind mountHost ownership vs container uidRun as the host uid, chown, or use a volume
Permission denied with correct chmod on RHELSELinux label missingAdd :z or :Z
invalid mount config for type "bind": source path does not exist--mount does not create the pathCreate it, or use -v deliberately
Empty directory appears on the host-v created a missing host path as rootPre-create with correct ownership; prefer --mount
Data gone after docker compose down -v-v removes named volumesUse down without -v
Disk fills with anonymous volumesdocker run -v /path without --rm -vdocker volume prune, declare named volumes
Config change has no effectBind mount hides the image's fileEdit the host file, or stop mounting over it
cannot mount volume over existing file, file existsMounting a directory onto a file pathMount the directory, or the file's parent
Very slow npm install on macOSBind mount through the VMAnonymous volume over node_modules
Restored volume starts with errorsBackup taken from a running databaseStop the service or use a logical dump

The design rule that keeps all of this simple: the container owns its data in a volume, the developer owns the source on the host, and nothing else is mounted. Every mount outside those two categories is either a secret that should be a secret store, or a workaround that will need explaining in six months.

Try matching browser tools

All tools

Small utilities that fit Docker work — they run entirely in this browser tab, so nothing you paste leaves your device.

All Docker guides

More in Docker

All Docker guides

The guides filed next to this one in Docker, in library order.