Sub topic 9
Giving the application writable paths with tmpfs and volumes
This chapter assumes you can run containers with docker run, read simple YAML, and know basic Linux file permissions. It also assumes you have seen --read-only in the previous chapter.
Why a read-only root needs writable exceptions
A read-only root filesystem blocks attackers from dropping binaries or editing config. Most applications still need to write somewhere: temporary files, sockets, caches, PID files, uploads. If you give them nothing, they crash with Read-only file system errors.
The fix is to leave the root read-only and mount small, specific writable paths on top of it. Everything else stays locked.
Option 1: tmpfs for scratch space
A tmpfs is a filesystem that lives in memory. It disappears when the container stops. It suits temporary data that does not need to survive a restart.
docker run --rm \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
--cap-drop ALL \
--security-opt no-new-privileges:true \
--user 10001:10001 \
python:3.12-slim \
sh -c 'touch /tmp/ok && echo "tmp is writable"; touch /etc/blocked || echo "root filesystem is not writable"'The mount options do real work:
| Option | Effect |
|---|---|
| `rw` | Allows writing. |
| `noexec` | Files in this mount cannot be executed. An attacker cannot write a binary there and run it. |
| `nosuid` | Setuid bits are ignored. |
| `size=64m` | Caps memory use. Without a cap, a runaway process can consume host RAM. |
The command prints tmp is writable, then root filesystem is not writable. The first touch succeeds because /tmp is a separate mount. The second fails because /etc is part of the read-only root.
Because tmpfs lives in memory, its size counts against the container's memory use. Set size deliberately.
Option 2: volumes for data that must persist
When data has to outlive the container, use a volume. A named volume is storage managed by Docker and mounted at a path you choose.
docker volume create appdata
docker run --rm \
--read-only \
--cap-drop ALL \
--user 10001:10001 \
-v appdata:/var/lib/app \
alpine sh -c 'echo saved > /var/lib/app/state.txt && cat /var/lib/app/state.txt'This usually fails on the first run with Permission denied. A new named volume is owned by root, and the process runs as user 10001. Fix this by setting ownership once, for example with a one-off container:
docker run --rm -v appdata:/data alpine chown 10001:10001 /dataRun the previous command again and it prints saved. In Kubernetes, the fsGroup field in the pod securityContext does this job.
Choosing between them
The same idea in Kubernetes
Kubernetes has no --tmpfs flag. You mount an emptyDir volume instead. An emptyDir is created empty when the pod starts and deleted when the pod goes away. Add medium: Memory to back it with RAM, and sizeLimit to cap it.
apiVersion: v1
kind: Pod
metadata:
name: writable-paths
spec:
securityContext:
runAsNonRoot: true
runAsUser: 10001
fsGroup: 10001
containers:
- name: app
image: python:3.12-slim
command: ["sleep", "3600"]
securityContext:
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop: ["ALL"]
volumeMounts:
- name: tmp
mountPath: /tmp
volumes:
- name: tmp
emptyDir:
medium: Memory
sizeLimit: 64MiUnlike Docker's --tmpfs options, an emptyDir mount does not set noexec for you. If you need that, enforce it through other controls, or avoid executing from that path.
Common mistakes
- Mounting a whole directory such as
/appas writable. Mount the narrowest path that works, such as/app/cache. - Forgetting the process user. A writable mount is useless if the non-root user cannot own it.
- Using
noexecon a path where the application unpacks and runs helpers. This breaks it. Test before you ship. - Leaving
sizeunset on tmpfs.
Exercise
A container runs with --read-only as user 10001. You mount a named volume at /var/lib/app, but writes fail with "Permission denied". The root filesystem is not the cause. What is the likely cause, and how do you fix it?
Show answer
A new named volume is owned by root, so user 10001 cannot write to it. Change the ownership of the volume to 10001:10001 (for example with a one-off chown container), or in Kubernetes set fsGroup: 10001 in the pod securityContext.