Interview's MateDocker
Interview's Mate

Docker

Compose · Networking · Security · Ops

Visualizer

DSA Visualizer
All 32 lessons
Custom Playground
Try your own examples

Learning

System Design
31 architecture topics
SQL Learning
160 foundations → production topics
AI Engineering
160 LLM → backend topics
Data Science
160 Python → MLOps topics
Data Engineer
160 SQL → streaming topics
Docker
160 Docker learning topics
Kubernetes
160 K8s fundamentals → cloud
Java LearnSoon
Coming soon
Python Learn
Interview Q&A & internals
JavaScript Learn
Closures, async & DOM Q&A
Tech Learn
Search, practise & track Q&A
AI DSA Practice
Solve coding problems
Theme
Home
Docker topics
Lesson 107/ 160
06. Docker Security & Hardening

Read-Only Root Filesystems (--read-only)

Make the container root filesystem read-only and mount writable tmp paths as needed.

Intermediate--read-only · tmpfs Writable3 quiz questions
Practice DevOps Q&A
In plain wordsDiagramTheoryStep by stepWhen to use itInterview cluesMistakes to avoidQuiz (3)Practice

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:prod for 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

Loading interactive 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.

Concrete --read-only setup

docker run --read-only --tmpfs /tmp:rw,noexec,nosuid,size=64m -v app-data:/var/lib/app --user 10001:10001 app:prod

Key takeaway: Prove tmpfs Writable with 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.

Deliberate failure test

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.

Key takeaway: Read-only root improves integrity; every legitimate writable path requires lifecycle and size controls.

How it works, step by step

Step 1 of 4
Design

1. 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.

Make writes an enumerated exception and prove application startup and rotation still work.
Key numbers at this step
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 recreation in 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

Explicit --read-only
Best for: Make the container root filesystem read-only and mount writable tmp paths as needed, with a reviewed runbook.

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.

Ambient default
Best for: Short-lived tmpfs Writable experiments with low blast radius.

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.

Managed Immutability
Best for: Teams that centralize Immutability policy across many workloads.

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:prod as proof and skipping touch /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:prod address --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?
Interview tip: Give one concrete operation-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

Loading quiz

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.

Open DevOps Q&A →Tech Learn Q&A
Previous topic
Preventing Privilege Escalation (--security-opt=no-new-privileges)
Next topic
Seccomp Profiles (Filtering Linux System Calls)