Volumes and Bind Mounts: Persisting Data Beyond the Container Lifecycle

Docker volumes vs bind mounts vs tmpfs: how each works, --mount vs -v, permissions, backups and which one to use for your data.

Executive Summary: A container’s own filesystem disappears when you remove the container, so anything that must survive belongs in a mount. Docker offers three kinds. Docker manages named volumes for you, so they are the right default for databases and application data. Bind mounts map a host directory into the container and suit local development and host-provided configuration. tmpfs mounts live in memory and suit scratch data and secrets that must never touch disk. Most data loss with Docker comes from not knowing which one you used.
Isometric database cylinder connected to a container and a host folder, showing named volumes and bind mounts

A developer runs Postgres in Docker, builds up a week of test data, upgrades the image with docker rm -f db and a fresh docker run, and the database is empty. Another mounts their project folder into a container, and the container’s node_modules vanishes because the empty host folder hides it. A third finds 40 GB of mysterious anonymous volumes on their laptop. All three are mount behaviors working as designed.

As covered in images, containers and layers, every container writes to a thin writable layer that disappears with the container. A mount attaches storage from outside that layer to a path inside the container, so data written there outlives the container and bypasses the copy-on-write overlay, which also makes it faster for write-heavy workloads.

The three mount types

Named volume Bind mount tmpfs
Where data lives Docker-managed area, /var/lib/docker/volumes/ on Linux Any path on the host you choose Host memory
Survives container removal Yes Yes (it is your directory) No
Pre-filled from the image Yes, when empty No, host content hides image content No
Portable across hosts and OSes Yes, managed by Docker and drivers No, depends on host paths Not applicable
Performance on Docker Desktop Fast (inside the VM) Slower (shared from the host OS) Fastest
Typical use Databases, uploads, persistent app data Source code in development, config files Scratch space, sensitive temporary files

Two syntaxes: -v and –mount

# Named volume
docker run -d -v pgdata:/var/lib/postgresql/data postgres:15
docker run -d --mount type=volume,source=pgdata,target=/var/lib/postgresql/data postgres:15

# Bind mount (read-only)
docker run -d -v "$PWD/nginx.conf":/etc/nginx/nginx.conf:ro nginx:1.23
docker run -d --mount type=bind,source="$PWD/nginx.conf",target=/etc/nginx/nginx.conf,readonly nginx:1.23

# tmpfs
docker run -d --mount type=tmpfs,target=/app/tmp,tmpfs-size=64m myapp

Both syntaxes do the same thing, with one important difference. With -v, Docker silently creates a missing host path as an empty directory, so a typo in a config file path gives the container an empty folder instead of an error. --mount fails loudly if the source does not exist. With -v, the first part is a volume name if it has no slash and a host path if it does. I prefer --mount in scripts and documentation because it is explicit, and -v for quick commands.

Named volumes: the default for data

docker volume create pgdata
docker volume ls
docker volume inspect pgdata
docker volume rm pgdata

Also, Docker creates a named volume automatically the first time a container references it. Its lifecycle is independent of any container: remove the Postgres container, start a new one with a newer image and the same -v pgdata:..., and the data is still there. That is how you upgrade database containers safely (after checking the database’s own upgrade notes, since a major Postgres version still needs pg_upgrade or a dump and restore).

Notably, when you mount an empty named volume at a path where the image already has files, Docker copies those files into the volume first. Bind mounts never do this; the host directory simply hides whatever the image had at that path.

Anonymous volumes and the VOLUME instruction

A VOLUME /var/lib/postgresql/data line in a Dockerfile, which many official database images include, tells Docker to create a volume at that path for every container, even if you did not ask for one. If you do not name it, Docker creates an anonymous volume with a random ID. Each new container gets a new one, the old ones stay behind, and that is where the mysterious gigabytes come from. Name your volumes explicitly, and use docker rm -v to remove a container together with its anonymous volumes.

Bind mounts: perfect for development

docker run --rm -it \
  --mount type=bind,source="$PWD",target=/app \
  --mount type=volume,target=/app/node_modules \
  -p 127.0.0.1:3000:3000 \
  node:18-bullseye-slim sh -c "npm ci && npm run dev"

Mounting the project directory lets the dev server inside the container see every edit you make on the host, so hot reload works. The second mount is a common trick: an anonymous volume at /app/node_modules stops the host’s folder from hiding the container’s dependencies, and keeps Linux-compiled native modules separate from any macOS ones on the host.

Permissions and user IDs

Linux file ownership is by numeric user ID. If the container runs as UID 1000 and the host files belong to UID 501, the container may not be able to write them, and files the container creates as root show up on the host owned by root. Options include running the container with your UID (--user "$(id -u):$(id -g)"), aligning the UID in the image with your host user for development, or using Docker Desktop, which maps ownership for you on macOS and Windows.

Performance on Mac and Windows

On Docker Desktop, bind mounts cross from the host OS into the Linux VM, which used to make large projects painfully slow. Recent Docker Desktop releases use VirtioFS on macOS, which is dramatically faster, but heavy directories such as node_modules, vendor and build output are still best kept in volumes inside the VM, as in the example above.

tmpfs: memory only

In contrast, a tmpfs mount lives in RAM and disappears when the container stops. Use it for scratch files that do not need persistence, to speed up workloads that write lots of temporary data, and for decrypted secrets that should never be written to disk. Combined with a read-only root filesystem, it lets an application write only where you allow, which container security hardening builds on.

Backing up and restoring a volume

# Back up the pgdata volume to a tarball in the current directory
docker run --rm -v pgdata:/data:ro -v "$PWD":/backup alpine:3.17 \
  tar czf /backup/pgdata-2023-05-16.tgz -C /data .

# Restore into a new volume
docker volume create pgdata_restored
docker run --rm -v pgdata_restored:/data -v "$PWD":/backup alpine:3.17 \
  tar xzf /backup/pgdata-2023-05-16.tgz -C /data

For databases, a file-level copy of a running database can be inconsistent. Stop the container first, or better, use the database’s own tools, such as pg_dump, for logical backups. Volumes make data persistent; they do not make it backed up.

Frequently Asked Questions

What is the difference between a Docker volume and a bind mount?

A volume is storage that Docker creates and manages, independent of the host’s directory layout, and Docker pre-fills it from the image when empty. A bind mount maps a specific host path into the container, so the container sees and changes the host’s files directly.

Which should I use for a database in Docker?

A named volume. It persists across container removal and upgrades, performs well, avoids host permission problems and you manage it with docker volume commands. Combine it with regular backups using the database’s own tools.

Why is my Docker disk usage growing with unnamed volumes?

Images that declare VOLUME create an anonymous volume for every container that does not mount a named one. However, old anonymous volumes remain after you remove the containers. Name your volumes, remove containers with docker rm -v, and review docker volume ls before pruning.

Does docker system prune delete volumes?

Not by default. docker system prune –volumes and docker volume prune remove unused volumes, which can include data from containers you have already removed, so check carefully before running them.

Choose the mount on purpose

Use named volumes for anything you would be upset to lose, bind mounts for code and config you edit on the host, and tmpfs for things that should never persist. Name every volume, prefer --mount when correctness matters, and back up data with the right tools. The container stays disposable, and the data stays put. For the full reference, see the Docker volumes documentation and the bind mounts documentation. Related reading on this site: Docker Compose.

Originally posted on 16 May 2023 and post last modified on 16 May 2023

DevOps Docker Docker Volumes Storage

Share this article

Leave a Reply

Your email address will not be published. Required fields are marked *