Skip to content
Tutorials Center
My learning

Topics

Lesson 48 of 111 ยท Data and Volumes

tmpfs and In-Memory Storage

Use Docker tmpfs mounts for fast, temporary, in-memory files: --tmpfs and --mount syntax, size limits, noexec, memory accounting, read-only containers and secrets.

Volumes and bind mounts store data on disk. A tmpfs mount is the third kind: a folder that lives entirely in the host's memory. Nothing is ever written to the container's writable layer or to a volume. When the container stops, the files are gone, and they were never on disk in the first place.

That sounds like a limitation, and it is, but it is exactly the right tool for three jobs: scratch space that should be fast, temporary files in a container whose root filesystem is read-only, and sensitive data that you do not want to leave behind on a disk. This lesson shows the syntax, the size limit you should always set, a default that surprises people (noexec), and how tmpfs memory is accounted for.

What tmpfs is

tmpfs is a Linux filesystem type that keeps its contents in RAM (and swap, if the host swaps). Docker mounts one at a path you choose inside the container.

TEXT
  Container filesystem
  โ”œโ”€โ”€ /            image layers + writable layer   (disk)
  โ”œโ”€โ”€ /data        volume                           (disk)
  โ”œโ”€โ”€ /app         bind mount                       (host disk)
  โ””โ”€โ”€ /scratch     tmpfs                            (memory only)

Compared with the container's writable layer, tmpfs has no copy-on-write overhead and no disk I/O. Compared with a volume, it needs no cleanup: stopping the container drops the data.

The two syntaxes

The short flag is --tmpfs; the long one is --mount type=tmpfs.

CONSOLE
$ docker run --rm --tmpfs /scratch:rw,size=16m,mode=1777 alpine sh -c 'df -h /scratch; mount | grep scratch'
Filesystem                Size      Used Available Use% Mounted on
tmpfs                    16.0M         0     16.0M   0% /scratch
tmpfs on /scratch type tmpfs (rw,nosuid,nodev,noexec,relatime,size=16384k)
CONSOLE
$ docker run --rm --mount type=tmpfs,dst=/cache,tmpfs-size=8m,tmpfs-mode=1770 alpine sh -c 'df -h /cache; ls -ld /cache'
Filesystem                Size      Used Available Use% Mounted on
tmpfs                     8.0M         0      8.0M   0% /cache
drwxrwx--T    2 root     root            40 Oct 23 14:34 /cache
Setting --tmpfs option --mount key
Maximum size size=16m tmpfs-size=16m (bytes, or with a unit such as 8m or 1g)
Permission bits of the folder mode=1777 tmpfs-mode=1777
Read-only ro readonly

Two differences between the forms matter in practice. --tmpfs takes a list of mount options exactly as the Linux mount command does, so any option the kernel accepts works. --mount type=tmpfs has a fixed set of keys, and, unlike --mount type=bind, it has no source: a tmpfs has nothing to read from. In Compose you can use either the short tmpfs: list or a volumes: entry with type: tmpfs; see Compose volumes.

You can confirm that nothing is stored anywhere. docker inspect shows the setting under HostConfig, and Mounts stays empty for the --tmpfs form:

CONSOLE
$ docker run -d --name demo --tmpfs /data alpine sleep 300
$ docker inspect -f '{{json .HostConfig.Tmpfs}} {{json .Mounts}}' demo
{"/data":""} []

Always set a size

Look at what happens when you do not:

CONSOLE
$ docker run --rm --tmpfs /plain alpine df -h /plain
Filesystem                Size      Used Available Use% Mounted on
tmpfs                     5.8G         0      5.8G   0% /plain

The default limit is half of the host's RAM. On this machine that is 5.8 GB, enough for a runaway program to starve everything else. A size limit turns the same runaway into a normal error:

CONSOLE
$ docker run --rm --tmpfs /ram:size=4m alpine sh -c 'dd if=/dev/zero of=/ram/f bs=1M count=10; echo exit=$?'
dd: error writing '/ram/f': No space left on device
5+0 records in
4+0 records out
4194304 bytes (4.0MB) copied, 0.004752 seconds, 841.8MB/s
exit=1

The program received "No space left on device" after exactly 4 MB, like on a small disk. Pick the limit from what the application needs plus a margin.

tmpfs memory counts against the container

The data really is memory. If you give the container a memory limit, tmpfs pages are charged to that same budget, even if the tmpfs size is larger:

CONSOLE
$ docker run --rm -m 32m --memory-swap 32m --tmpfs /x:size=100m alpine sh -c 'dd if=/dev/zero of=/x/f bs=1M count=60'
$ echo "docker exit=$?"
docker exit=137

The tmpfs said 100 MB was fine, but the container was limited to 32 MB. The kernel killed the process (exit code 137 = 128 + SIGKILL 9) and dd printed nothing. This is an out-of-memory kill, and it is the most common tmpfs surprise: when a tmpfs fills up, the container may be killed instead of seeing "No space left on device". Set size below the container's memory limit, and leave room for the program itself.

Without -m, a large tmpfs competes with every other process on the host. Treat size as a promise about memory you are willing to spend, per container.

tmpfs is noexec by default

The mount line above contained nosuid,nodev,noexec. Docker applies these safe defaults, so a program written to a tmpfs cannot be run from it:

CONSOLE
$ docker run --rm --tmpfs /x alpine sh -c 'printf "#!/bin/sh\necho hi\n" > /x/s.sh; chmod +x /x/s.sh; /x/s.sh'
sh: /x/s.sh: Permission denied

This blocks a common attack: an intruder downloads a payload into a writable temporary folder and runs it. If you legitimately need to run files from there (a language runtime that unpacks native libraries into /tmp, for example), add exec explicitly:

CONSOLE
$ docker run --rm --tmpfs /x:exec alpine sh -c 'printf "#!/bin/sh\necho hi\n" > /x/s.sh; chmod +x /x/s.sh; /x/s.sh; mount | grep " /x "'
hi
tmpfs on /x type tmpfs (rw,nosuid,nodev,relatime)

The noexec flag is gone from the second mount line. Only do this for the path that needs it.

Data does not survive a restart

A tmpfs belongs to one running container. When that container stops, or even restarts, the contents disappear:

CONSOLE
$ docker run -d --name demo --tmpfs /data alpine sleep 300
$ docker exec demo sh -c 'echo secret > /data/f; cat /data/f'
secret
$ docker restart -t 1 demo
$ docker exec demo ls -A /data | wc -l
0

Two containers cannot share a tmpfs either: each gets its own. If several processes need the same in-memory files, run them in one container, or use a volume with the tmpfs driver option.

Where tmpfs earns its place

Read-only containers

A hardened container runs with --read-only, so its root filesystem cannot be modified. Many programs still need somewhere to write: PID files, sockets, caches, /tmp. Give them a tmpfs for exactly those paths:

CONSOLE
$ docker run --rm --read-only --tmpfs /tmp alpine sh -c 'touch /tmp/ok && echo tmp ok; touch /etc/nope'
tmp ok
touch: /etc/nope: Read-only file system

/tmp accepts writes; the rest of the filesystem refuses. If the application fails after you add --read-only, its error message tells you which path it wanted to write to. Add a tmpfs for that path. This pattern appears again in Container Security.

Secrets and sensitive scratch files

A decrypted key, a session cache or a one-time token written to the container's writable layer or a volume stays on disk after the container is stopped, and may survive in a backup. In a tmpfs it exists only while the container runs and never touches the disk (unless the host swaps memory out; on sensitive hosts disable swap or use encrypted swap).

For a secret that comes from outside, Compose and Swarm already mount secrets as in-memory files under /run/secrets; see Secrets. Use your own tmpfs for secrets the program generates.

Fast scratch space and tests

Databases in integration tests are slow mostly because of disk synchronization. Putting the data directory on a tmpfs makes a throwaway PostgreSQL much faster:

Terminal
docker run -d --name test-db \
  -e POSTGRES_PASSWORD=test \
  --tmpfs /var/lib/postgresql/data:rw,size=512m \
  postgres:17-alpine

The data vanishes with the container, which is what a test run wants. Never do this with data you need to keep.

Compose equivalent

YAML
services:
  app:
    image: alpine
    command: sleep 300
    read_only: true
    tmpfs:
      - /tmp:size=16m,mode=1777
      - /run

When to use tmpfs and when not to

Use it for Avoid it for
Temporary files that can be lost Anything that must survive a restart
/tmp, /run and caches in --read-only containers Data larger than a safe share of the host's RAM
Secrets generated at runtime Data shared between containers
Throwaway test databases Windows containers (tmpfs is a Linux feature)

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What is stored on disk when a container writes 10 MB into a --tmpfs mount?

    A tmpfs lives in RAM (and swap, if the host swaps). It never reaches the writable layer or a volume, and it disappears when the container stops.
  2. 2. You omit size= on a --tmpfs mount. What is the default limit?

    The kernel default for tmpfs is half of the physical memory, 5.8 GB on the test machine. Always set an explicit size.
  3. 3. A container has -m 32m and a tmpfs with size=100m. A process writes 60 MB into the tmpfs. What most likely happens?

    tmpfs memory is charged to the container. Exceeding the container's limit triggers an out-of-memory kill, so keep the tmpfs size well below the memory limit.
  4. 4. A script copied into a --tmpfs /work directory fails with "Permission denied" when you execute it, even after chmod +x. Why?

    Docker applies nosuid, nodev and noexec to tmpfs mounts. Use --tmpfs /work:exec only for paths that really must run programs.
  5. 5. Which pairing is a good use of tmpfs?

    Read-only containers still need a few writable, temporary paths. A small tmpfs provides them without making the whole filesystem writable.

Summary

  • A tmpfs mount keeps files in memory only; nothing goes to the writable layer or a volume, and everything vanishes when the container stops or restarts.
  • Use --tmpfs /path:size=... or --mount type=tmpfs,dst=/path,tmpfs-size=....
  • The default size is half the host's RAM, so always set size.
  • tmpfs memory counts against the container's memory limit; overfilling can end in an out-of-memory kill (exit 137).
  • Docker mounts tmpfs noexec,nosuid,nodev; add exec only where needed.
  • Typical uses: /tmp and /run in --read-only containers, runtime secrets and fast throwaway test data.

Next: Backup and Restore