Sub topic 7
In-memory storage with tmpfs
This chapter assumes you can run containers with docker run, know the difference between an image and a container, and have met volumes and bind mounts in earlier chapters. It covers the third mount type, the tmpfs mount.
What a tmpfs mount is
A tmpfs mount gives a container a directory that lives in the host's memory instead of on disk. Nothing is written to the container's writable layer or to a volume. When the container stops, Docker discards the contents.
Use a tmpfs mount for data that must not outlive the container and should be fast to read and write. Typical cases are scratch files, session caches, and secrets that you do not want written to disk.
Creating a tmpfs mount
The --tmpfs flag takes a path in the container and optional comma-separated settings after a colon.
docker run --rm --tmpfs /scratch:rw,size=64m alpine \
sh -c 'echo temp > /scratch/file && df -h /scratch'The command does the following:
- It mounts a tmpfs at
/scratch, readable and writable, limited to 64 MB. - It writes a file into it.
- It runs
df -h, which reports a filesystem of typetmpfswith a 64 MB size.
The container exits and --rm removes it. The file is gone, and nothing remains on the host.
The --mount form
You can also write the same mount with --mount. This form uses explicit key-value pairs.
docker run --rm \
--mount type=tmpfs,target=/scratch,tmpfs-size=67108864 \
alpine sh -c 'df -h /scratch'Here tmpfs-size is given in bytes. Unlike --tmpfs, --mount type=tmpfs does not accept a source, because there is nothing on the host to point at. Use --tmpfs for quick commands. Use --mount when you want every option spelled out, for example in scripts.
Limits and common errors
- No size limit by default. Without
size, a tmpfs can grow until it uses a large share of host memory. Set a size so a runaway process cannot exhaust RAM. - Memory counts against the container. Data in a tmpfs is charged to the container's memory. If you set a memory limit with
--memory, writing to the tmpfs can trigger an out-of-memory kill. - Data is not shared. A tmpfs belongs to one container. Another container cannot mount the same one, so
--volumes-fromdoes not apply. - Swap can still write it to disk. If the host swaps memory out, tmpfs pages may reach the swap device. A tmpfs is not a guaranteed way to keep secrets off disk.
- Linux only. tmpfs mounts work for Linux containers. They are not available for Windows containers.
- Overlays existing content. If the target path already holds files from the image, the tmpfs hides them while it is mounted. The image files return only in a new container without the mount.
Choosing tmpfs
| Need | Best choice |
|---|---|
| Data must survive container removal | Named volume |
| Share host files with the container during development | Bind mount |
| Temporary data that should be fast and never saved | tmpfs mount |
Exercise
You run docker run --name job --tmpfs /cache:size=32m alpine sh -c 'echo x > /cache/a', and the container exits. You then run docker start job and list /cache in the restarted container. Will a be there, and why?
Show answer
No. A tmpfs mount lives only while the container is running. When the container stopped, Docker discarded the contents. On restart, the mount is created again and is empty, even though the container itself was never removed.
Exercise
Why should you usually set size on a tmpfs mount?
Show answer
Without a size, the tmpfs can grow to use a large part of the host's memory. A size limit stops a runaway process from exhausting RAM.