Difficulty: Intermediate
Compare Docker volumes and bind mounts. When would you use each, and where does tmpfs fit in?
Volumes are storage areas created and managed by Docker (on Linux under /var/lib/docker/volumes/), referenced by name and independent of the host's directory layout. Bind mounts map an exact host path into the container, so the container sees and changes the host's files directly. Use volumes for persistent application data such as databases in production, and bind mounts for local development where you want code edits on the host to show up instantly inside the container.
| Aspect | Named volume | Bind mount | tmpfs mount |
|---|---|---|---|
| Where data lives | Docker-managed area | Any host path you choose | Host memory only |
| Managed with | docker volume CLI | Normal host tools | Nothing to manage |
| Survives container removal | Yes | Yes (it is the host's file) | No |
| Pre-populated from image | Yes, if the volume is empty | No, it hides the image's directory | No |
| Portability | High, no host path assumptions | Depends on host layout and OS | High |
| Typical use | Postgres data, uploads | Source code hot reload, config files | Secrets, scratch files |
/app can hide the node_modules that were installed during the build.docker rm -v or --rm runs. docker compose down -v removes the project's volumes, which can wipe a local database.Permission denied. Volumes avoid most of this because Docker creates them.node_modules many teams still use a volume.-v name:/path and -v /host/path:/path are short and common. --mount type=volume,src=pgdata,dst=/var/lib/postgresql/data is explicit and preferred in scripts. One real difference: with -v, a missing host path is silently created as an empty directory; with --mount type=bind Docker returns an error, which catches typos.
A typical local setup for a Node API: bind mount ./src for hot reload, a named volume for Postgres data so it survives docker compose down, and a tmpfs mount for temporary files. In production on Kubernetes, the equivalent of a named volume is a PersistentVolumeClaim, and bind mounts (hostPath) are usually avoided for application data.
| Named volume | Bind mount | tmpfs | |
|---|---|---|---|
| Backing storage | Docker volume directory | Exact host path | RAM |
| Best for | Databases, uploads | Live code editing | Secrets, scratch data |
| Removed with container | No | No | Yes |
| Hides image files at path | Only if volume not empty | Always | Always |
# Named volume for durable database data
docker volume create pgdata
docker run -d --name db \
--mount type=volume,src=pgdata,dst=/var/lib/postgresql/data \
-e POSTGRES_PASSWORD=devpass postgres:17
# Bind mount for live-editing source code (read-only inside container)
docker run -d --name api \
--mount type=bind,src="$(pwd)/src",dst=/app/src,readonly \
my-api:dev
# tmpfs for scratch files that must never touch disk
docker run -d --name worker --mount type=tmpfs,dst=/tmp my-worker
docker volume ls
docker volume inspect pgdata # shows Mountpoint on the host
The --mount form makes the mount type explicit and fails loudly if a bind source path does not exist.
services:
api:
build: .
volumes:
- ./src:/app/src # bind mount: hot reload
- node_modules:/app/node_modules # volume keeps container deps
db:
image: postgres:17
environment:
POSTGRES_PASSWORD: devpass
volumes:
- pgdata:/var/lib/postgresql/data
volumes:
pgdata:
node_modules:
The named node_modules volume stops the host bind mount from hiding dependencies installed in the image, and pgdata survives docker compose down unless you add -v.
Named Volumes, Bind Mounts, tmpfs Mounts, --mount flag, Data Persistence