If you have to persist or share files with a container, the choice comes down to two things Docker treats very differently. A bind mount maps an exact path on the host into the container — you pick the path, you own the files, and the container sees whatever is there. A named volume is a chunk of storage Docker creates and manages for you under /var/lib/docker/volumes, referenced by name instead of by path. The short rule: bind-mount your project source code while you develop it, and use a named volume for a database’s data directory or anything else that just needs to survive and be portable. This guide walks the trade-offs — portability, backups, permissions, and Desktop performance — so you can pick with confidence and wire it up correctly.
What you’ll accomplish
- Tell a bind mount and a named volume apart by who owns the path and the file lifecycle.
- Pick the right mount type for source code, database data, config files, and certificates.
- Write both the modern
--mountsyntax and the shorthand-v, and know when each matters. - Mount things read-only and back a named volume up to a portable archive.
- Recognize where each choice bites you — Desktop performance, permissions, and portability.
Prerequisites
- Docker Engine 27 or 28 (the commands are unchanged across both). Confirm with
docker --version. - Compose v2 if you use the Compose example — it ships as the
docker composesubcommand, not the olddocker-composebinary. - A shell on the host where the engine runs. On Docker Desktop (macOS/Windows) the “host” is a Linux VM, which matters for performance and paths — this guide calls out where that changes the answer.
- Basic familiarity with running a container. If
docker runis new, start with our container basics material and come back.
How it works
A container’s own filesystem is a stack of image layers plus a thin writable layer. Anything written to that writable layer disappears when the container is removed. To keep data, or to hand a file in from outside, you mount storage from the host into a path inside the container. Docker gives you two mechanisms for that, shown side by side below.
With a bind mount, you name a host path — say /home/dev/project — and Docker maps it straight through. There is no indirection: the container reads and writes the real files on your host, with the host’s ownership and permissions. That is exactly what you want for live source code, because an edit in your editor is instantly visible inside the container. It is also what you want for handing in a single config file or a TLS certificate.
With a named volume, Docker allocates a directory it controls under /var/lib/docker/volumes/<name>/_data and mounts that in. You never touch the host path directly; you refer to the volume by name. Docker owns its lifecycle — it survives docker rm, it is listed by docker volume ls, and it is initialized with the right ownership from the image on first use. That indirection is a feature: the volume is portable across hosts, easy to back up by name, and not tied to a directory layout you have to reproduce elsewhere.
The mechanism decides the trade-offs. The decision tree below is the same logic in flowchart form.
Step-by-step
-
Create and inspect a named volume. Volumes can be created explicitly or on first use. Create one by name:
docker volume create appdataappdataInspect it to see where Docker put the data and what driver it uses:
docker volume inspect appdata[ { "CreatedAt": "2026-07-24T14:02:11Z", "Driver": "local", "Labels": null, "Mountpoint": "/var/lib/docker/volumes/appdata/_data", "Name": "appdata", "Options": null, "Scope": "local" } ]The
Mountpointis inside Docker’s managed area — you refer toappdataby name, never by that path. -
Mount the named volume with the explicit
--mountsyntax. The--mountflag is verbose but unambiguous, which is why it is the recommended form for anything non-trivial. Run a database against the volume:docker run -d --name db \ --mount type=volume,source=appdata,target=/var/lib/mysql \ -e MYSQL_ROOT_PASSWORD=example \ mysql:83f9c2b1a7e0d5c8f4a6b9d2e1f0c7a3b5d8e6f4c2a1b0d9e8c7f6a5b4d3c2e1fA 64-character container ID means it started. Docker initialized
appdatawith MySQL’s data directory on first write. -
Bind-mount source code with
--mount type=bind. For live development, map your project directory in. Thesourcemust be an absolute host path:docker run -d --name web \ --mount type=bind,source="$(pwd)"/src,target=/app \ -w /app node:22 npm run devWith
type=bind, Docker errors out if the host path is missing — a useful guard against typos. (With-v, a missing path is silently created as an empty directory, which hides the mistake.) -
Use the
-vshorthand when you want brevity. The same two mounts in shorthand form — note the field order issource:target[:options]:docker run -d --name db -v appdata:/var/lib/mysql -e MYSQL_ROOT_PASSWORD=example mysql:8 docker run -d --name web -v "$(pwd)"/src:/app -w /app node:22 npm run devDocker decides bind vs volume from the source: a path (starting with
/or.) is a bind mount; a bare name is a volume. That inference is exactly why--mountis safer for scripts — it never guesses. -
Mount read-only where the container should not write. Append
:ro(orreadonlyunder--mount) for config files, certs, or any input the app must not modify:docker run --rm \ --mount type=bind,source="$(pwd)"/nginx.conf,target=/etc/nginx/nginx.conf,readonly \ nginx:1.27Any write inside
/etc/nginx/nginx.confnow fails with a read-only filesystem error, which is what you want for a mounted config. -
Declare both in a Compose service. A typical dev stack bind-mounts the code and uses a named volume for the database. In
compose.yaml:services: web: image: node:22 working_dir: /app command: npm run dev volumes: - ./src:/app # bind mount: live source - /app/node_modules # anonymous volume: keep container's deps db: image: mysql:8 environment: MYSQL_ROOT_PASSWORD: example volumes: - appdata:/var/lib/mysql # named volume: database data volumes: appdata:Bring it up:
docker compose up -d[+] Running 3/3 ✔ Network app_default Created ✔ Container app-db-1 Started ✔ Container app-web-1 Started
Validate it works
Confirm the volume exists and is bound to the right container. List volumes:
docker volume ls
DRIVER VOLUME NAME
local appdata
Write a file through the container, remove the container, then start a new one on the same volume and confirm the data survived:
docker run --rm -v appdata:/data alpine sh -c 'echo persisted > /data/proof.txt'
docker run --rm -v appdata:/data alpine cat /data/proof.txt
persisted
For the bind mount, list the mount point from inside the container and confirm it matches the host directory:
docker run --rm --mount type=bind,source="$(pwd)"/src,target=/app alpine ls /app
index.js
package.json
Seeing your real project files means the host path is mapped through correctly.
Troubleshooting
- “Permission denied” writing to a bind mount. The container process runs as a UID (often not root) that does not own the host files. Docker does not remap ownership for bind mounts — the host’s UID/GID show straight through. Fix by
chown-ing the host directory to the container’s UID, or run the container with--user "$(id -u):$(id -g)"so it writes as you. Named volumes sidestep this: Docker seeds them with the image’s ownership on first use. - A named volume looks empty after you changed the image. A volume is initialized from the image’s contents only when it is empty on first mount. If the volume already had data, the new image’s files are not copied in. Confirm the target path with
docker volume inspectand, if you really want a clean slate, remove and recreate the volume — after backing it up. -vsilently created a directory I did not mean to mount. With the-vshorthand a missing bind source becomes a new empty directory, so a typo mounts nothing useful. Switch to--mount type=bind, which fails loudly when the source path does not exist.- Slow file access on Docker Desktop. On macOS and Windows, bind mounts cross the host-to-VM boundary and can be markedly slower for large, chatty trees like
node_modulesor a big Git repo. Keep dependency directories in a named (or anonymous) volume instead of the bind-mounted tree, and enable Desktop’s VirtioFS/gRPC-FUSE file sharing. On native Linux there is no VM, so bind mounts run at host speed and this does not apply. - Disk fills up with orphaned volumes. Volumes are not removed when their container is, by design. Over time unused ones accumulate. List dangling volumes with
docker volume ls -f dangling=trueand reclaim space withdocker volume prune— but read the name first, because prune does not ask twice.
When this is the wrong approach
Bind mounts and named volumes both assume a single host’s local disk. If you need the same data available to containers across several hosts, the local driver is the wrong tool — reach for a volume plugin backed by NFS, a cloud block store, or a clustered filesystem, and treat the mount type as a detail below that. For secrets, neither a plain bind mount nor a volume is ideal: use Docker secrets or your orchestrator’s secret store so the value is not sitting in a world-readable file. And for throwaway scratch space that must never touch disk — think a decrypted key held only in memory — use a tmpfs mount instead of either option here.
For a broader conceptual comparison, see Docker volumes vs bind mounts. To make volume data survivable, follow back up and restore Docker volumes, and if orphaned data is eating your disk, clean up a full overlay2 directory. The official references are the volumes documentation and the bind mounts documentation.
If this is the kind of work you’d rather never own again, it’s the daily job of Managed Cloud Ops — book an infrastructure review.