Tmpfs Mounts

Difficulty: Intermediate

Question

What is a tmpfs mount in Docker, and when would you use one instead of a volume or bind mount?

Answer

A tmpfs mount is a filesystem that lives only in the host's memory. Docker mounts it at a path inside the container, data written there never touches the host disk or the container's writable layer, and it disappears the moment the container stops. It is used for scratch files that must be fast, temporary, or must never be persisted, such as session caches or decrypted secrets.

How it compares to the other mount types

VolumeBind mounttmpfs
Backed byDocker-managed directory on diskAny host pathHost RAM (can swap)
Survives container stopYesYesNo
Shareable between containersYesYesNo
Typical useDatabase data, uploadsSource code in dev, configCaches, temp files, secrets
PlatformAllAllLinux containers only

Where tmpfs genuinely helps

  • Read-only containers. Running with --read-only is a strong hardening step, but many apps still need to write to /tmp or /run. A tmpfs on just those paths keeps the rest of the filesystem immutable.
  • Sensitive data. A payment service that decrypts a key or writes a short-lived token can keep it in RAM, so it never lands on disk where a backup or a stolen disk image could leak it.
  • Fast scratch space. Build caches, image thumbnails being processed, or test databases that are recreated on every run benefit from memory-speed I/O and do not wear out SSDs.
  • Avoiding layer bloat. Writes to the container layer go through the storage driver's copy-on-write path; tmpfs skips that entirely.

Syntax

Two equivalent forms exist. --tmpfs /app/cache:size=64m is short, and --mount type=tmpfs,destination=/app/cache,tmpfs-size=67108864,tmpfs-mode=1770 is explicit and is the style Docker recommends. In Compose use the tmpfs: key on a service or a volumes entry with type: tmpfs.

Gotchas

  • Size is unlimited unless you set it. A runaway process can fill RAM. Always set size / tmpfs-size.
  • It counts against memory. Pages in a tmpfs are charged to the container's memory cgroup, so a 1 GB tmpfs inside a container limited to --memory=512m will get the container OOM-killed long before the tmpfs is full.
  • Not a security vault. tmpfs pages can be written to swap unless swap is disabled or encrypted, and anyone with root on the host can read the container's memory and mounts.
  • Not for Docker Desktop on Windows containers. tmpfs is a Linux kernel feature.
  • Data is gone on restart. Never put anything there you cannot regenerate.

Code examples

Read-only container with writable tmpfs paths

docker run -d --name api \
  --read-only \
  --tmpfs /tmp:rw,size=64m,mode=1777 \
  --mount type=tmpfs,destination=/app/cache,tmpfs-size=134217728 \
  --memory=512m \
  my-api:1.4

# Confirm the mounts are memory-backed
docker exec api df -h /tmp /app/cache

The root filesystem is immutable while /tmp and /app/cache are writable RAM-backed paths with hard size caps.

tmpfs in Docker Compose

services:
  worker:
    image: thumbnailer:2.0
    read_only: true
    tmpfs:
      - /tmp:size=32m
    volumes:
      - type: tmpfs
        target: /work
        tmpfs:
          size: 268435456   # 256 MiB in bytes

Both the short `tmpfs` list and the long volume syntax create in-memory mounts; the long form takes size in bytes.

Key points

  • tmpfs lives in host RAM, is private to one container and vanishes when the container stops.
  • Always set a size; the default is unlimited.
  • tmpfs usage is charged against the container's memory limit.
  • Pair tmpfs with --read-only for hardened containers that still need /tmp.
  • tmpfs can swap to disk, so it reduces but does not eliminate secret exposure.

Common follow-up questions

  • What happens if a tmpfs grows larger than the container's memory limit?
  • How is a tmpfs mount different from /dev/shm?
  • How would you run a container with a read-only root filesystem?
  • Where would you store secrets instead of environment variables?

Interview tips

  • Lead with the three mount types and place tmpfs among them; a comparison shows structured thinking.
  • Give a real use case like --read-only plus tmpfs /tmp; it signals production hardening experience.
  • Mentioning the memory cgroup charge and swap caveat sets you apart from rote answers.

Concepts covered

tmpfs, In-memory Filesystem, --mount type=tmpfs, Read-only Containers, Secrets Handling

More Docker & Containers questions