TechEarl

Docker Volumes vs Bind Mounts (and When to Use Each)

Three ways to persist data with Docker: named volumes, bind mounts, and tmpfs. What each one actually does, where the bytes live on disk, the right choice for databases, dev workflows, and caches, and why bind mounts are slow on Mac and Windows.

Ishan Karunaratne⏱️ 11 min readUpdated
Share thisCopied
Named volumes, bind mounts and tmpfs explained: where the bytes live, the right pick for databases, dev and caches, backups, and fixing slow Mac bind mounts.

Use a named volume for databases and anything stateful, a bind mount when you edit files on the host and want the container to see them, and tmpfs for scratch space that should vanish on restart. All three look similar at the CLI (-v something:/path/in/container) but behave very differently. This page covers each one, backups, permission errors, and why bind mounts are slow on Mac and Windows. Most "I lost my data" disasters trace back to picking the wrong one for the job.

The short version

Mount typeWhat it isBest for
Named volume (-v name:/path)Docker-managed storage at a path Docker choosesDatabases, app state, anything you want to persist and you don't care where it lives
Bind mount (-v /host/path:/path)A host directory mapped into the containerEditing source code from the host, host-side backups, sharing config files
Anonymous volume (-v /path)An unnamed Docker-managed volumeAlmost never. Use a named volume instead.
tmpfs (--tmpfs /path)In-memory onlyScratch space, caches that don't need to survive container restart

For databases and other stateful services, default to named volumes. For development where you're editing files on the host and want changes reflected in the container, bind mounts. For everything else, named volumes.

Named volumes

bash
docker run -d -v pgdata:/var/lib/postgresql/data postgres:17

Docker creates a volume named pgdata (if it doesn't exist) and mounts it at /var/lib/postgresql/data inside the container. The actual bytes live in /var/lib/docker/volumes/pgdata/_data/ on a Linux host, or inside the Docker Desktop VM on Mac/Windows.

The key properties:

  • Survives container removal. docker rm does not touch the volume. You can stop, remove, and recreate the container; the data is intact.
  • Survive even docker compose down, but not docker compose down -v (the -v flag explicitly removes named volumes).
  • Docker manages the on-disk location, you don't pick the path, you pick the name. Cleaner ownership, no UID surprises on Mac/Windows.

Manage them:

bash
docker volume ls                    # list
docker volume inspect pgdata        # driver, mountpoint, labels
docker volume rm pgdata             # remove (must not be in use)
docker volume prune                 # remove all unused (data loss; asks for confirmation)

Bind mounts

bash
docker run -d -v /home/me/myproject:/app node:26 npm start

Maps the host directory /home/me/myproject to /app inside the container. Both directions: changes inside the container modify the host directory; changes on the host appear inside the container.

The key properties:

  • You choose the host path. Perfect for "this is my project; edit it on the host."
  • Performance varies by platform. Native filesystem access on Linux is fast. Docker Desktop on Mac/Windows uses a virtualized filesystem that's slower, especially with thousands of small files.
  • UID/GID matters. A container process writing to a bind mount creates files owned by whatever UID it ran as (root by default). Those files appear on the host with that ownership. Mac/Windows abstract this away to some extent; Linux exposes it directly.

Modes:

bash
-v /host/path:/path:ro        # read-only
-v /host/path:/path:rw        # read-write (default)
-v /host/path:/path:z         # SELinux relabel (private)
-v /host/path:/path:Z         # SELinux relabel (shared)

When to use which

Databases → named volume, always.

bash
docker run -d --name pg -v pgdata:/var/lib/postgresql/data postgres:17
  • The container's UID 999 (or whatever Postgres runs as) needs to own the data directory; named volumes handle this transparently.
  • On Mac/Windows, bind-mounted database directories are noticeably slower and occasionally hit permission issues.
  • You don't need direct host filesystem access to the database files; you'd use pg_dump instead.

Source code in development → bind mount.

bash
docker run -d -v "$(pwd):/app" -w /app node:26 npm run dev
  • You edit on the host with your editor, the container picks up changes immediately for hot reload.
  • Files created by the container appear on the host (a build output, a generated migration). That's usually what you want.

The hybrid: bind-mount source + named volume for node_modules.

yaml
services:
  app:
    image: node:26
    volumes:
      - .:/app
      - app-node_modules:/app/node_modules

volumes:
  app-node_modules:

This is the trick that keeps dev fast on Mac/Windows. The source bind mount gives you live editing; the named volume overlay on node_modules keeps that directory in a Docker-managed location instead of the slow bind-mount space. Without this, npm install and any tool that walks node_modules is dramatically slower.

Config files → bind mount, read-only.

bash
docker run -d -v ./nginx.conf:/etc/nginx/conf.d/default.conf:ro nginx:alpine
  • One file, edited on the host, mounted into the container.
  • :ro prevents the container from modifying your host file.

Caches and tmpfiles → tmpfs.

bash
docker run -d --tmpfs /tmp:size=100m my-app
  • Lives in RAM, not on disk. Disappears when the container stops.
  • Useful for scratch space or temp files that shouldn't survive a restart.

Backups

Named volume backup:

bash
docker run --rm \
  -v pgdata:/source:ro \
  -v "$(pwd):/backup" \
  alpine tar czf /backup/pgdata-$(date +%Y%m%d).tar.gz -C /source .

That spins up a throwaway alpine container with the volume mounted at /source and the host's current directory at /backup, then tars the volume contents to a host file.

Restore:

bash
docker run --rm \
  -v pgdata:/restore \
  -v "$(pwd):/backup" \
  alpine tar xzf /backup/pgdata-20260601.tar.gz -C /restore

For databases specifically, prefer the database's own dump tool (pg_dump, mysqldump, mongodump), those produce portable, version-independent backups. Volume-level backups require stopping the container for consistency and are tied to the specific database version.

Where named volumes live on disk

  • Linux: /var/lib/docker/volumes/<name>/_data
  • macOS: inside Docker Desktop's virtual disk (~/Library/Containers/com.docker.docker/Data/...). Not directly accessible from macOS.
  • Windows: inside Docker Desktop's WSL2 distro's filesystem (\\wsl$\docker-desktop-data\...).

Full picture for macOS in Where Are Docker's Files on a Mac?.

Permission errors on bind mounts (and how to avoid them)

The container's UID writing to a bind mount creates files owned by that UID on the host. By default, that UID is root. On a Linux host, ls -la shows the file as owned by root; you need sudo rm to delete it.

Two fixes:

bash
# Run the container as your host user
docker run --rm -u "$(id -u):$(id -g)" -v "$(pwd):/app" -w /app node:26 npm test
bash
# After the fact (Linux)
sudo chown -R $(id -u):$(id -g) .

This is why named volumes are nicer for stateful services on Mac/Windows, they sidestep the UID question entirely.

Why bind mounts are slow on Mac and Windows

On Linux, a bind mount is free: the kernel maps one path to another on the same filesystem, at native speed. On Mac and Windows, the Linux container runs inside Docker Desktop's VM, and your project lives on macOS (APFS) or Windows (NTFS). For the container to see a file:

  1. The macOS/Windows kernel reads the file.
  2. Docker Desktop's file-sharing layer (VirtioFS, gRPC FUSE, or the older osxfs) translates between the host filesystem and the Linux VM's filesystem.
  3. The Linux kernel hands the bytes to the container.

Every read and write crosses that boundary. For one file it's a millisecond of overhead; for node_modules with 100,000 small files, those milliseconds add up to seconds or minutes. It has gotten much better, and the remaining slowness mostly hits dependency directories. Five fixes, roughly in order of payoff:

Confirm you're on VirtioFS (Mac)

code
Docker Desktop → Settings → General → "Choose file sharing implementation for your containers"
  • VirtioFS: default in Docker Desktop 4.22+ (August 2023). Significantly faster, typically 2-4x on real workloads with lots of small files.
  • gRPC FUSE: the older default, slower.
  • osxfs (legacy): much older, very slow.

If you updated Docker Desktop from a much older version, the setting may have stayed on the old default, so check it. If VirtioFS is somehow slower for your workload, fall back to gRPC FUSE. Windows has no comparable switch: the WSL2 backend shares files over 9P (the WSL filesystem protocol), so where the project lives matters more.

Put node_modules in a named volume

This is the hybrid pattern above: bind-mount the source for hot reload, then mount a named volume over node_modules. The named volume lives in the VM's native filesystem, so no translation is needed and npm install (and anything that walks the tree) runs at full speed. The trade-off is that your host editor can't see the container's node_modules for "go to definition" into a dependency; if you need that, install node_modules on the host too (the two copies are then separate). The same pattern works for __pycache__, Composer's vendor/, Bundler's vendor/bundle and Cargo's target/: any large auto-generated dependency tree.

Keep the project inside WSL2 (Windows)

  • Project in C:\Users\you\code\project (Windows filesystem): every bind-mount read crosses the 9P translation boundary. Slow.
  • Project in \\wsl$\Ubuntu\home\you\code\project (inside WSL2): the container reads the file natively from the WSL2 distro's filesystem. Fast.
bash
# Open your WSL2 distro (e.g., Ubuntu)
cd ~
git clone https://github.com/.../my-project.git
cd my-project
docker compose up -d

Edit with VS Code's Remote-WSL extension or another editor that supports WSL paths. Same Windows machine, dramatically faster Docker.

Mount only what the container needs

docker run -v $(pwd):/app bind-mounts everything: a 500 MB .git, a coverage directory, yesterday's dist. All of it crosses the bridge whenever something walks the tree. Use .dockerignore for builds, and mount only the live-edit directories at runtime:

yaml
volumes:
  - ./src:/app/src
  - ./public:/app/public
  - ./package.json:/app/package.json:ro

More verbose, but faster. For caches that don't need to survive a restart, a tmpfs mount (--tmpfs /app/cache:size=200m) runs at memory speed on every platform.

The old cached and delegated flags

yaml
volumes:
  - .:/app:cached       # host source is authoritative; container reads may lag
  - .:/app:delegated    # container's writes may lag the host

These came from the gRPC FUSE era and traded consistency for speed. Docker Desktop ignores them on VirtioFS. Leave them in old Compose files (no harm), but don't add them to new ones.

Even with all of this, Mac and Windows bind mounts stay slower than native Linux, because the containers run in a VM. Optimize what you can; for the absolute fastest dev loop, Linux remains best.

Common pitfalls

  • Using an anonymous volume by accident. -v /var/lib/postgresql/data (no name on the left of the colon) creates an anonymous volume. It's persistent, but you can't easily reference it later. Use a named volume.
  • docker compose down -v wiping data. The -v flag removes named volumes. Don't run it casually on stacks with state you care about.
  • Bind mount over a directory that contains useful image content. Mounting ./empty-dir:/app/wp-content on a WordPress container overlays the image's default wp-content, hiding the bundled themes. Named volumes copy the image content into the volume on first init; bind mounts do not.
  • Permission flips on Mac. Docker Desktop's file sharing handles the UID translation transparently but occasionally produces odd ownership. Restart Docker Desktop, or move to a named volume.
  • Editing on the Windows side while the container runs in WSL2. That's the slow case. Move the project into WSL2.
  • Trusting a volume to survive everything. docker volume rm and docker volume prune both remove volumes. The volume is durable across container removal, not across explicit volume operations.

What to do next

FAQ

Named volumes: yes, by default. Bind mounts: yes, the data is on the host, untouched by container operations. Anonymous volumes: yes by default, but they're hard to find later. The data-loss commands are docker volume rm, docker volume prune, and docker compose down -v.

Bind mounts on Mac are slower because Docker Desktop runs Linux in a VM and bridges your Mac filesystem into it through a virtualized layer. Named volumes live inside the VM's filesystem natively, fast. For large directories (especially node_modules, __pycache__, .git), the difference is noticeable.

Yes, mount the same named volume into multiple containers. They all see the same files. The same applies to bind mounts. Useful for sharing config or static assets between services.

Spin up a throwaway container with the volume and a host directory mounted, then tar the volume contents to the host directory. Recipe in the Backups section above. For databases, prefer the database's own dump tool, portable, version-independent.

Yes if you do active Docker development. WSL2-side files don't cross the Windows-to-Linux translation boundary, so bind mounts are fast. Edit with VS Code's Remote-WSL extension and you barely notice you're working through WSL.

Functionally the same on Linux, both use the host's filesystem directly. The performance difference is only on Mac and Windows where Docker Desktop runs Linux in a VM and bind mounts cross the VM boundary.

Sources

Authoritative references this article was fact-checked against.

TagsDockerVolumesBind MountsPersistenceStoragePerformanceVirtioFSDevOps

Found this useful? Pass it on.

Copied

Ishan Karunaratne

Systems and Network Architect · Chief Technology Officer

Systems and network architect and Chief Technology Officer with more than two decades designing, building, and running production software, cloud and network architecture, Linux systems, and the bare metal underneath them, and lately working AI into the stack. A US Army veteran who served in Operation Iraqi Freedom. What I write here is drawn from the full arc of that work, across architecture, engineering, and operations, not any single job.

Keep reading

Related posts

Learn Docker Compose with a working docker-compose.yml: services, volumes, networks and service-name DNS, bridge vs host networking, and host.docker.internal.

Docker Compose: Getting Started with docker-compose.yml

A working docker-compose.yml that runs a web app and a database together, the commands you need (up, down, logs, exec), how containers find each other on Docker networks, passing environment variables (ARG vs ENV, -e, env_file), and the V1-to-V2 change that breaks a lot of older tutorials.

Build RAG with embeddings, chunking, vector search and reranking, plus semantic search in MySQL 9 with the VECTOR type. When long context replaces RAG.

How to Build RAG with Embeddings and Vector Search

Build RAG with embeddings, chunking, and vector search. Covers OpenAI / Voyage / Cohere embeddings, chunk sizes, semantic search in MySQL 9 with the VECTOR type, hybrid search, reranking, and when long context replaces RAG entirely.

docker run cheat sheet: every flag by job, core Docker commands, and run recipes for MySQL, MariaDB, PostgreSQL, Redis, Nginx, PHP, WordPress and more.

docker run Cheat Sheet: Flags and Common Patterns

Every docker run flag I actually use, grouped by job, the rest of the Docker CLI on one cheat sheet, and copy-paste recipes for Nginx, Apache, PHP, MySQL, MariaDB, PostgreSQL, MongoDB, Redis, Elasticsearch, MinIO, WordPress, Adminer and a plain Linux shell.