Bind mounts and tmpfs mounts

Intermediate · 10 min

Map a host directory into a container for live code and config, or give it memory-only scratch space. Syntax differences, read-only mounts, and the traps.

Before thisNamed volumes

Pål-Kristian Hamre · 10 min

Bind mounts and tmpfs mounts

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Pål-Kristian HamreReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A bind mount maps a path on the Docker host into the container. Both sides see the same files live, which suits source code in development and config files.
  • -v creates a missing host directory, owned by root. --mount type=bind fails instead, unless you add bind-create-src, which catches typos.
  • Paths are resolved on the daemon's host, not the client's, so bind mounts do not work against a remote engine the way they do locally.
  • Mount config read-only with :ro or readonly. A bind mount hides whatever the image had at that path, like any mount.
  • Bind-mounting a single file breaks when an editor saves by replacing the file, because the container keeps the old inode. Mount the directory instead.
  • A tmpfs mount lives in memory and disappears when the container stops. Docker mounts it noexec, nosuid and nodev, and its contents count against the container's memory limit.

Sometimes the host has to see the files

Named volumes are Docker’s storage, deliberately kept away from you. Some files you want to edit on the host and see in the container immediately: the source code of the app you are developing, an nginx.conf, a TLS certificate managed by the host. And some data you never want on disk at all: a session cache, a decrypted secret, scratch files. Bind mounts cover the first case and tmpfs mounts the second.

Bind mounts

docker run --rm -it -p 3000:3000 \
  --mount type=bind,src="$(pwd)",dst=/app \
  -w /app node:24-slim npm run dev

The container’s /app is your working directory. Save a file in your editor and the dev server inside sees the change at once. Both sides are reading the same files, so nothing is copied.

The equivalent -v "$(pwd)":/app differs in one way the bind mounts page spells out. If the host path does not exist, -v creates it as a directory, owned by root. --mount refuses with “bind source path does not exist”, unless you add bind-create-src. The refusal is a feature: a typo in a path is an error instead of a mysteriously empty directory.

Bind mounts are resolved on the machine running the Docker daemon, not the one running the CLI. On a remote engine, -v ./src:/app refers to a ./src on the server. On Docker Desktop the daemon is in a Linux VM, and Desktop shares your Mac or Windows files into it, which is why bind-mounted file access there is slower than a named volume.

Mount options worth knowing

--mount type=bind,src=/etc/notes,dst=/etc/notes,readonly     # or -v /etc/notes:/etc/notes:ro
  • readonly. Config the container must not change. The docs note that read-only applies recursively to submounts only on kernel 5.12 or later, controlled by bind-recursive.
  • SELinux z and Z (with -v). On Fedora or RHEL hosts with SELinux enforcing, a container cannot read a host directory until it is relabelled. z marks it shared between containers, Z private to one. The docs warn that this relabels the host files themselves, and that Z on something like /home or /usr can make the host unusable. The SELinux lesson explains the labels.
  • bind-propagation. Whether mounts created later under the path show up on the other side. Almost nobody needs to change the default, rprivate.

A bind mount hides whatever the image had at the target, exactly like a volume, but nothing is ever copied into it. Bind-mount an empty directory over /app and the image’s /app is simply invisible.

Two traps

Single files and editors. You can bind-mount one file, such as -v ./nginx.conf:/etc/nginx/nginx.conf:ro. A bind mount pins the file’s inode. Many editors save by writing a new file and renaming it over the old one, which creates a new inode, and the container keeps seeing the old one until it restarts. Mount the directory, ./nginx/:/etc/nginx/conf.d/, and edits always show up.

node_modules and friends. Bind-mounting a project hides the dependencies the image installed in /app/node_modules. The usual fix is a second mount just for that path, an anonymous or named volume, so the image’s copy is used. Compose’s develop.watch, in the Compose development lesson, syncs files instead of mounting and avoids the problem.

File ownership across the boundary is its own topic, the next lesson.

tmpfs mounts

docker run --read-only \
  --mount type=tmpfs,dst=/tmp,tmpfs-size=64m \
  --tmpfs /run:size=16m \
  notes:1.4.2

A tmpfs mount is the kernel’s in-memory filesystem. Nothing is written to the host’s disk, and everything is gone when the container stops. The tmpfs page lists the details:

  • Linux only, and cannot be shared between containers.
  • With --mount, the default size limit is half the host’s RAM, and the mode defaults to 1777. Set tmpfs-size.
  • Data stored there counts towards the container’s memory limit, so a full tmpfs can get the container killed for running out of memory, as resource limits explains.
  • tmpfs pages can be swapped out, so “never touches disk” does not hold if the host has swap.

One default the tmpfs page’s option table does not make clear: Docker adds noexec, nosuid and nodev to every tmpfs mount it creates. I found it in the moby daemon source, where the tmpfs options always start from those three. A tool that unpacks and runs a binary in /tmp will get “Permission denied”. With --tmpfs you can pass exec to override it.

The pairing in the example is a common hardening pattern: --read-only for the root filesystem, plus tmpfs for the few paths that genuinely need scratch writes.

Choosing

Need Use
Application data, databases Named volume
Source code during development, host-managed config Bind mount, read-only where possible
Scratch space, caches, decrypted secrets tmpfs

About the video

The embed is Pål-Kristian Hamre’s “Docker Volumes Explained: Bind Mount vs Named Volume vs tmpfs”, March 2026, 10 minutes. Its chapters cover why Docker deletes your data, bind mounts, named volumes, a comparison, and tmpfs. It is from a small channel and spends about four minutes on bind mounts and one on tmpfs, so the options and traps above come from the docs. English was judged from the title, the description and the English caption track. Automation Avenue’s video in deeper compares -v and --mount with docker inspect.

What was run: the tmpfs default options were read from the moby source, daemon/oci_linux.go, which prepends noexec, nosuid and nodev, and daemon/container/container_unix.go, which routes both --tmpfs and --mount type=tmpfs through that path. Nothing was mounted, since there is no Docker here. The rest is from the bind mounts and tmpfs pages in the docker/docs repository, checked 2026-11-28.

Where this fits

Bind mounts bring the host’s owners and permissions with them, and that is where most “Permission denied” errors start: file ownership and permissions in mounts.

Check yourself

  1. 01You run docker run -v ./confg:/etc/app ... with a typo in the host path. The app starts with an empty config directory. Why, and how would --mount have helped?

    -v silently creates the missing host directory, so the container gets an empty mount. --mount type=bind fails with 'bind source path does not exist', which exposes the typo.

  2. 02You bind-mount ./nginx.conf to /etc/nginx/nginx.conf, edit it on the host, and reload nginx. It still uses the old config. Why?

    Many editors save by writing a new file and renaming it over the old one. The bind mount still points at the original inode, so the container sees the old content. Mount the containing directory instead.

  3. 03A tool extracts a binary into a --tmpfs /tmp mount and then fails with Permission denied when running it. Why?

    Docker mounts tmpfs with noexec by default, so nothing on it can be executed. Pass the exec option, for example --tmpfs /tmp:exec, if that is really needed.

  4. 04Why is a bind mount a poor choice for a production database's data directory?

    It depends on a specific host path and its permissions, other host processes can change the files underneath the database, and on Docker Desktop it goes through file sharing to the VM, which is slower. A named volume avoids all three.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark