Docker Volumes: bind mount, named volume, and tmpfs
A container's filesystem vanishes on docker rm and breaks database data. See the three volume types, when to use each, UID permissions, and backup.
You spin up a Postgres container, create the tables, seed it with test data, and everything works. Then you run docker rm to clean up and, on the next start, the database is empty. That's not a bug: a container's filesystem is ephemeral by design. Each container is a thin writable layer on top of the read-only image layers, and that layer dies with the container. Anything written there — database files, uploads, logs — disappears on docker rm, and that breaks exactly the things you most need to keep.
The fix is to move the data out of the container layer and into a place where Docker (or the host) guarantees persistence. There are three mechanisms for that — bind mount, named volume, and tmpfs — and picking the wrong one is expensive. This post covers all three, when to use each, the permission gotcha that bites everyone, and how to back up a volume.
Why the container filesystem is ephemeral
An image is a stack of read-only layers. When you run docker run, Docker stacks a writable layer on top — the container layer. Everything the process writes to the filesystem goes into that layer via copy-on-write. It exists as long as the container exists. docker stop keeps the layer (the container is merely stopped), but docker rm deletes it, and docker rm -f does both at once.
The practical problem shows up in any redeploy flow. Shipping a new image is, in effect, creating a new container from an image — new container layer, old data gone. So nothing that must survive a redeploy can live in the container layer. Database data in particular is the classic case: Postgres writes to /var/lib/postgresql/data, and if that path sits in the container layer, a docker rm wipes the whole database.
Bind mount: map a host path
A bind mount connects a directory (or file) from the host directly to a path inside the container. Docker manages nothing — it's the host filesystem showing up in there.
docker run -v /home/rafael/app:/app node:20
Everything in /home/rafael/app on the host appears at /app in the container, and writes on both sides see each other instantly. That's what makes the bind mount the right tool for code hot-reload in development: you edit a file in your editor, the container sees the change immediately, nodemon/vite reloads. No rebuild, no copy.
The cost is coupling. A bind mount depends on an absolute path existing on the host, so the same compose won't behave identically on your machine and a teammate's without agreeing on a folder structure. And here comes the permission gotcha: the process inside the container runs with a UID (often root, UID 0, or an app UID like 1000), and the files on the bind mount belong to the owner on the host. When the UIDs don't match, you get permission denied on write, or worse, files created by the container end up owned by root on your machine and you need sudo to delete them. In dev that's manageable; in production, it's a headache factory.
Named volume: the right choice for real data
A named volume is managed by Docker. It lives in an area controlled by the daemon (on Linux, under /var/lib/docker/volumes/), and you reference it by name, not by host path.
docker volume create pgdata
docker run -v pgdata:/var/lib/postgresql/data postgres:16
In Compose it's explicit and portable:
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
This is the right path for any data that must persist: Postgres, MySQL, Redis with persistence, user uploads. The volume survives a docker rm of the container — you destroy and recreate the container freely, and the data stays put, attached by name. It's portable (no dependency on a specific host path), Docker handles the initial permissions better than a bind mount does, and you can back it up with standard commands. If you're torn between bind mount and named volume for data, the rule is simple: real data goes in a named volume.
When volumes get nested inside a large compose, alongside networks and configs, the YAML indentation hides subtle errors — a volumes: key at the wrong level and Docker mounts somewhere else. To check how the volumes are declared, converting the compose from YAML to JSON makes the nesting explicit and easy to audit.
tmpfs: in memory, gone on stop
A tmpfs mount lives in the host's RAM, never touches disk, and disappears when the container stops. It's for sensitive or temporary data you don't want persisted anywhere.
docker run --tmpfs /run/secrets:size=64m alpine
The use cases are secrets decrypted at runtime, throwaway caches, and temporary processing files. Since nothing goes to disk, there's no risk of a secret leaking into an image layer or a forgotten volume. The trade-off is obvious: nothing survives docker stop, and the content consumes RAM. Use it sparingly and always with a size set.
Permissions, UID, and the most common error
Most volume problems aren't Docker configuration — they're UID. The process in the container has a UID; the files on the volume or bind mount have an owner. When they don't match, you get permission denied.
With a named volume, Docker usually initializes the volume with the correct permissions on first mount (the Postgres image, for example, chowns the data directory). With a bind mount, the owner is whoever already exists on the host, and Docker doesn't touch it. The fix that works is to align the UIDs: run the container with the same UID as the file owner (docker run --user 1000:1000), or adjust the owner on the host to the UID the container process uses. Avoid the shortcut of running everything as root just to make the error go away — you'd be trading a permission problem for a security one.
Backing up a volume
A named volume is backed up with a throwaway container that mounts the volume and packages the content:
docker run --rm -v pgdata:/data -v $(pwd):/backup alpine \
tar czf /backup/pgdata.tar.gz -C /data .
The alpine container comes up only to run tar, writes the .tar.gz into the current directory via a second bind mount, and disappears (--rm). To restore, you reverse it: mount the target volume and extract the tarball inside. For databases, keep in mind that a tar of live files can capture an inconsistent state — for Postgres, prefer pg_dump on top, or stop the container before taring.
If you're still setting up your dev environment and want to see volumes in the context of a full stack, the post on Docker Compose for development shows how to wire services, networks, and volumes into a single file.
Frequently asked questions
Bind mount or named volume for the database?
Named volume, always. A bind mount couples the data to a host path and exposes you to permission and portability problems. The named volume is managed by Docker, survives docker rm, is portable across machines, and has standard backup commands. The bind mount is for code in dev.
Does the data disappear when I stop the container?
It depends on where it is. If it's in the container layer, it's gone on docker rm (but not on docker stop — a stopped container keeps the layer). If it's in a named volume or bind mount, it survives both stop and rm. If it's in a tmpfs, it's gone as soon as the container stops, because it lives only in RAM.
Why do I get "permission denied" writing to the volume?
Almost always a UID mismatch: the process inside the container runs with a UID that has no permission over the volume or bind mount files. Align the UIDs with --user on the container or by adjusting the file owner on the host. Running as root hides the symptom but opens a security hole.
How do I back up a named volume?
Spin up a throwaway container that mounts the volume and a host backup directory, and run tar inside. The volume becomes /data, the destination becomes /backup, tar packages it, and the container vanishes with --rm. For databases, a logical dump (pg_dump) is safer than a tar of live files.
What to take away
Never trust the container filesystem for anything that must survive a redeploy — it's ephemeral by design and gone on docker rm. Use a bind mount for code in development, where hot-reload pays for the coupling to the host. Use a named volume for real data: databases, uploads, anything persistent. Reserve tmpfs for secrets and temporaries that should die with the container. And when you hit permission denied, look at the UID first, not the Docker configuration.
- 01 Does the PageSpeed Score Affect SEO? 100/100 and Failing Google doesn't rank on PageSpeed's 0 to 100 score but on Core Web Vitals from real users. Here's why a site scores 100/100 and still fails in the field.
- 02 Unit test vs integration test: the difference in practice The difference between unit and integration tests with API and database examples: what each one catches, what it costs, and when to use each in your project.