TExcelTAD
Home › Docker Compose › Bind Mounts & tmpfs

Bind Mounts & tmpfs

A named volume is storage that Docker owns. A bind mount is the opposite: you pick a file or folder on the host, and the container sees exactly that path, live. A tmpfs mount is a third kind that lives only in memory. Each has a clear job:

Mount type Data lives in Survives container removal Typical use
named volume Docker's storage area yes databases, uploads, anything the app owns
bind mount a host path you choose yes (it's your folder) source code in development, config files, sockets
tmpfs host RAM no caches, temp files, secrets you never want on disk

Bind mounts in development

The classic use: mount your source folder into the container so the dev server sees every edit without a rebuild.

compose · try it yourself
# $ docker compose config --no-path-resolution
services:
  web:
    image: node:24-alpine
    working_dir: /app
    command: ["npm", "run", "dev"]
    volumes:
      - ./src:/app/src
      - ./package.json:/app/package.json:ro
name: demo
services:
  web:
    command:
      - npm
      - run
      - dev
    image: node:24-alpine
    networks:
      default: null
    volumes:
      - type: bind
        source: ./src
        target: /app/src
        bind: {}
      - type: bind
        source: ./package.json
        target: /app/package.json
        read_only: true
        bind: {}
    working_dir: /app
networks:
  default:
    name: demo_default

We passed --no-path-resolution so the output stays readable. Without it, Compose turns every relative path into an absolute one, based on the folder that contains the compose file: ./src becomes something like /home/you/projects/demo/src. That is why a compose file works from any current directory, and why docker compose -f other/compose.yaml up mounts other/src, not ./src.

Tip: relative paths are resolved against the project directory, which is the folder of the first -f file. Keep the compose file at the repository root and every ./ path is easy to reason about.

Long syntax options

compose · try it yourself
# $ docker compose config --no-path-resolution
services:
  web:
    image: node:24-alpine
    volumes:
      - ./config/app.json:/app/config.json:ro,z
      - type: bind
        source: ./uploads
        target: /app/uploads
        bind:
          create_host_path: false
      - type: bind
        source: /var/run/docker.sock
        target: /var/run/docker.sock
        read_only: true
name: demo
services:
  web:
    image: node:24-alpine
    networks:
      default: null
    volumes:
      - type: bind
        source: ./config/app.json
        target: /app/config.json
        read_only: true
        bind:
          selinux: z
      - type: bind
        source: ./uploads
        target: /app/uploads
        bind:
          create_host_path: false
      - type: bind
        source: /var/run/docker.sock
        target: /var/run/docker.sock
        read_only: true
networks:
  default:
    name: demo_default
  • :ro / read_only: true stops the container from changing your files. Use it for every config file.
  • :z / :Z (bind.selinux) relabels the path for SELinux hosts (Fedora, RHEL). z means shared between containers, Z private to this one. On other hosts it has no effect.
  • create_host_path: with the short syntax, a missing host path is created as an empty directory (owned by root). That bites when you meant a file: ./config/app.json missing on the host turns into an empty directory called app.json, and the app fails with a confusing "is a directory" error. The long syntax with create_host_path: false fails fast instead, with invalid mount config for type "bind": bind source path does not exist.
  • Mounting /var/run/docker.sock gives the container full control of the Docker daemon, which is root on the host. Read-only doesn't help much (the API still accepts commands). Only do it for trusted tools; see Compose Security.

The node_modules problem

Mount ./ over /app and the container's node_modules (installed during the image build) disappears, hidden behind your host folder. Two standard fixes:

compose · try it yourself
# $ docker compose config --no-path-resolution
services:
  web:
    build: .
    volumes:
      - .:/app
      - node_modules:/app/node_modules

volumes:
  node_modules:
name: demo
services:
  web:
    networks:
      default: null
    volumes:
      - type: bind
        source: .
        target: /app
        bind: {}
      - type: volume
        source: node_modules
        target: /app/node_modules
        volume: {}
    build:
      context: .
      dockerfile: Dockerfile
networks:
  default:
    name: demo_default
volumes:
  node_modules:
    name: demo_node_modules

Mounts are applied from the shortest target to the longest, so /app/node_modules (a named volume, filled from the image on first use) sits on top of the bind mount at /app. The container keeps its Linux-built dependencies, and your host's node_modules (perhaps built for macOS) never leaks in. The cleaner modern alternative is not to bind-mount at all and use develop & watch to sync only source files.

A model of how overlapping mounts resolve a path:

python · try it yourself
mounts = [
    ("/app",              "bind ./ (host)"),
    ("/app/node_modules", "volume node_modules"),
    ("/app/.cache",       "tmpfs"),
]

def resolve(path):
    # the mount with the longest matching target wins
    best = None
    for target, source in mounts:
        if path == target or path.startswith(target + "/"):
            if best is None or len(target) > len(best[0]):
                best = (target, source)
    return best[1] if best else "image filesystem"

for p in ["/app/src/index.js", "/app/node_modules/vite/package.json",
          "/app/.cache/build.json", "/usr/local/bin/node"]:
    print(f"{p:<38} -> {resolve(p)}")
/app/src/index.js                      -> bind ./ (host)
/app/node_modules/vite/package.json    -> volume node_modules
/app/.cache/build.json                 -> tmpfs
/usr/local/bin/node                    -> image filesystem

File ownership

Bind mounts keep the host's numeric user and group IDs. If your host user is UID 1000 and the container runs as UID 1000 too (the node user in the official images), everything just works. If the container runs as root, files it creates in your project folder are owned by root and you need sudo to delete them. Set user: "${UID:-1000}:${GID:-1000}" or use an image user that matches. Docker Desktop on macOS and Windows translates ownership for you, so this mostly hurts on Linux.

tmpfs mounts

A tmpfs mount is a RAM disk: fast, private to the container, gone on stop. Two ways to declare one:

compose · try it yourself
services:
  api:
    image: ghcr.io/acme/api:3.0
    read_only: true
    tmpfs:
      - /run:size=8m,mode=755
    volumes:
      - type: tmpfs
        target: /app/.cache
        tmpfs:
          size: 32m
          mode: 0o1777
name: demo
services:
  api:
    image: ghcr.io/acme/api:3.0
    networks:
      default: null
    read_only: true
    tmpfs:
      - /run:size=8m,mode=755
    volumes:
      - type: tmpfs
        target: /app/.cache
        tmpfs:
          size: "33554432"
          mode: 1023
networks:
  default:
    name: demo_default

Notice how Compose normalises the long form: 32m becomes a byte count (33554432) and the octal mode 0o1777 is printed as the decimal number 1023. They're the same value; just don't be surprised when you diff the output.

The pair read_only: true + tmpfs is a strong hardening pattern: the container's root filesystem becomes read-only, and only the few paths that must be writable are small RAM disks. An attacker who gets into the process can't drop files into /usr/bin.

Note: always give tmpfs a size. Without one it can grow to half the host's RAM, and memory used by tmpfs counts against the container's memory limit.

Choosing a mount type

python · try it yourself
def choose(needs_persistence, edited_on_host, sensitive, shared_between_hosts):
    if shared_between_hosts:
        return "volume with a network driver (NFS, cloud) or an external store"
    if sensitive and not needs_persistence:
        return "tmpfs (or a secret)"
    if edited_on_host:
        return "bind mount"
    if needs_persistence:
        return "named volume"
    return "tmpfs or nothing (container filesystem)"

cases = {
    "Postgres data":               (True, False, False, False),
    "source code while developing": (True, True, False, False),
    "nginx.conf":                  (True, True, False, False),
    "session cache":               (False, False, True, False),
    "uploads on 3 web servers":    (True, False, False, True),
}
for what, args in cases.items():
    print(f"{what:<30} -> {choose(*args)}")
Postgres data                  -> named volume
source code while developing   -> bind mount
nginx.conf                     -> bind mount
session cache                  -> tmpfs (or a secret)
uploads on 3 web servers       -> volume with a network driver (NFS, cloud) or an external store

For config files that don't change while the app runs, configs are often a better fit than bind mounts, because they also work in Swarm.

Learn more: Named Volumes · Docker Volumes · develop & watch

Test yourself

With the short syntax ./nginx.conf:/etc/nginx/nginx.conf:ro, what happens if ./nginx.conf doesn't exist on the host?

  1. Compose refuses to start
  2. Docker creates an empty directory named nginx.conf on the host and mounts it
  3. The image's nginx.conf is used
  4. An empty file is created

A service has .:/app and node_modules:/app/node_modules. Where does the container read /app/node_modules/x from?

  1. The host folder
  2. The named volume, because the longer target is mounted on top
  3. The image only
  4. It's random

What is a good reason to use tmpfs?

  1. Database storage
  2. Sharing files with the host
  3. Fast scratch space that must never be written to disk, e.g. with read_only: true
  4. Sharing data between hosts