Read-Only Root Filesystems (--read-only)
Make the container root filesystem read-only and mount writable tmp paths as needed.
In plain words
Make writes an enumerated exception and prove application startup and rotation still work.
Think of it like…
The app is delivered with a sealed floor; only a signed-off in/out tray and scratchpad are writable.
Where you’ve seen it
In practice, docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod is the concrete move for this job: make the container root filesystem read-only and mount writable tmp paths as needed. The release is not accepted until touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation confirms the result.
Words you’ll hear
- --read-only
- The implementation control in
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prodfor make the container root filesystem read-only and mount writable tmp paths as needed. - tmpfs Writable
- The result to observe with
touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation, rather than infer from a successful command alone. - Immutability
- The production boundary exposed when mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.
Visual diagram
Theory & deep dive
--read-only control path
The concrete mechanism is docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod. The observable result is touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation; if those disagree, inspect the runtime evidence before changing unrelated settings.
Make the container root filesystem read-only and mount writable tmp paths as needed. Keep --read-only, tmpfs Writable and Immutability in the same deployment record so the intent and the observed state cannot drift apart.
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod
touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation, not with command success alone.Immutability failure boundary
Libraries that write caches, update CA stores or create /run files fail under read-only root. Adding a broad writable volume at / recreates the problem, and a writable bind mount can still modify sensitive host files. The narrow failure to design for is mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.
Accept this compromise explicitly: Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls. The controlled check-touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation-also identifies the point where a different design is safer.
Exercise this failure safely: mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.
How it works, step by step
Step 1 of 41. State the boundary
Write down why the workload needs this: Libraries that write caches, update CA stores or create /run files fail under read-only root. Adding a broad writable volume at / recreates the problem, and a writable bind mount can still modify sensitive host files.
- Intent
- --read-only
When to use it & trade-offs
What it is
--read-only mounts the container root filesystem read-only, while an application can receive explicit writable tmpfs or named-volume paths for /tmp, runtime PID files and data. Image immutability then blocks many post-compromise persistence attempts.
Why it matters: Libraries that write caches, update CA stores or create /run files fail under read-only root. Adding a broad writable volume at / recreates the problem, and a writable bind mount can still modify sensitive host files.
When to reach for it
- The fit is strongest for this job: make the container root filesystem read-only and mount writable tmp paths as needed. Pair it with
touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreationin the runbook. - Production --read-only changes should make tmpfs Writable observable through
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod, rather than trusting the runtime default. - Keep a rollback example for the case where mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.
When to avoid or be careful
- Do not accept command success while the system can still fail by mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.
- Avoid erasing Immutability protection merely to dodge this cost: “Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls”.
- Do not replace the evidence path-
touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation-with a screenshot or a mutable tag.
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod: practical choices
Strengths: --read-only mounts the container root filesystem read-only, while an application can receive explicit writable tmpfs or named-volume paths for /tmp, runtime PID files and data. Image immutability then blocks many post-compromise persistence attempts.
Watch out: Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls.
Strengths: It can begin with docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod and fewer surrounding controls.
Watch out: It silently accepts mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup, which makes the result hard to reproduce after promotion.
Strengths: Libraries that write caches, update CA stores or create /run files fail under read-only root. Adding a broad writable volume at / recreates the problem, and a writable bind mount can still modify sensitive host files.
Watch out: The platform adds an ownership and debugging layer; retain touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation as the direct check.
Interview signals & numbers to know
When this topic comes up
- docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod
- touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation
- Failure signature: mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup
- Trade-off: Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls.
Benchmarks & rules of thumb
- 2 writable exceptions
- tmpfs and data volume in the example
- 64 MiB
- bounded scratch memory
- 0 writes
- allowed to image rootfs through normal filesystem calls
Common mistakes & follow-ups
Common interview mistakes
- Treating a successful
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prodas proof and skippingtouch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation. - Mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup
- Optimizing --read-only without accepting that read-only root improves integrity; every legitimate writable path requires lifecycle and size controls.
Likely interviewer follow-ups
- Why does
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prodaddress --read-only here rather than through application code? - How would you distinguish healthy tmpfs Writable from the failure “mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup” using
touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation? - When would this trade-off-“Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls”-justify a different Immutability design?
docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod-and one proof-touch /etc/should-fail, write /tmp/check and verify only /var/lib/app survives recreation. Then name the boundary: mounting /var/run as a broad host bind or disabling read-only because one package wants to update itself at startup.Knowledge check
Put it into practice
Practice related interview Q&A
Apply Read-Only Root Filesystems (--read-only) with generative AI interview Q&A and hands-on practice.