Read-only root filesystem and writable paths
A read-only container root filesystem prevents normal writes to the image-backed root and writable layer. Applications may still need particular writable mounts. A temporary filesystem (tmpfs) can provide scratch space without using a persistent volume for that data.
Worked example
This is an offline lesson. Scenarios are fictional and Docker commands are unexecuted. Any optional execution requires an approved disposable VM, a reviewed preloaded image with verified digest, blocked external access and appropriate resource limits. No host-policy change, real credentials or live scan is required. Keep the exercise offline. Before any separately authorized runtime check, verify the effective daemon, builder, runner or deployment identity as applicable; use synthetic data, reviewed inputs and bounded resources. Record missing evidence as NOT RUN.
docker run --rm --pull=never --network none \ --user 10001:10001 --cap-drop ALL \ --security-opt no-new-privileges=true --read-only \ --tmpfs /tmp:rw,noexec,nosuid,nodev,size=16m,mode=1777 \ busybox:1.36 sh -c 'printf ok > /tmp/check; cat /tmp/check'This staged Linux example needs a reviewed local image and compatible runtime. Predicted output is ok. The application writes only into the bounded scratch mount; the root remains read-only. tmpfs contents disappear when the container stops; host swap may still be involved depending on configuration. noexec restricts direct execution from that mount; it is not a complete code-execution defense. Explicit volumes and bind mounts have their own permissions, so --read-only does not make all mounts read-only. For a real service inventory required write paths, choose temporary or durable storage deliberately and test behavior with the intended permissions. Do not write to host directories for this worksheet. The scratch write is only a positive control. A separate authorized negative test must target a known normally writable path on the root filesystem rather than the scratch mount; distinguish read-only filesystem failure from ordinary UID permission failure. The command bounds scratch storage but not overall memory or process consumption.
Common mistake
A read-only root does not mean a stateless application or prevent reading secrets. Review every mounted path and data lifecycle separately.
Manual practice
A fictional app needs temporary files and a persistent report. Design two different storage choices, and state what should happen when the container stops or is removed.
Worksheet: lab-fixtures/docker-container-security/08-read-only-runtime/scenario.txt.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Read-only root filesystem and writable paths — Knowledge check
Apply defensive container security reasoning in a fictional scenario.
Does --read-only make a separate writable volume read-only?
Where should disposable scratch data go in this example?
What does noexec alone establish?