Difficulty: Intermediate
What is a tmpfs mount in Docker, and when would you use one instead of a volume or bind mount?
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.
| Volume | Bind mount | tmpfs | |
|---|---|---|---|
| Backed by | Docker-managed directory on disk | Any host path | Host RAM (can swap) |
| Survives container stop | Yes | Yes | No |
| Shareable between containers | Yes | Yes | No |
| Typical use | Database data, uploads | Source code in dev, config | Caches, temp files, secrets |
| Platform | All | All | Linux containers only |
--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.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.
size / tmpfs-size.--memory=512m will get the container OOM-killed long before the tmpfs is full.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.
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.
tmpfs, In-memory Filesystem, --mount type=tmpfs, Read-only Containers, Secrets Handling