Sub topic 10
Read-only Root Filesystems and Resource Limits
This chapter assumes you can run containers with docker run, know what a Linux process and cgroup are (from the isolation chapter), and have read the chapters on non-root users and dropped capabilities. Here you will limit what a compromised container can write and how much of the host it can consume.
Why restrict writes and resources
Two common attack outcomes are persistence and exhaustion. With a writable root filesystem, an attacker who gets code execution can drop tools, modify binaries, or plant scripts inside the container. With no resource limits, one runaway or hostile process can use all host memory, CPU, or process slots and take down every other container.
A read-only root filesystem blocks the first outcome. cgroup limits blunt the second.
Making the root filesystem read-only
The --read-only flag mounts the container's root filesystem as read-only. Most applications still need somewhere to write temporary files, so you add a tmpfs mount. A tmpfs is an in-memory filesystem that disappears when the container stops.
docker run -d --name web \
--user 10001:10001 \
--cap-drop ALL \
--security-opt no-new-privileges \
--read-only --tmpfs /tmp \
--memory 256m --cpus 0.5 --pids-limit 100 \
myapp:1.0This command combines the earlier restrictions with the new ones. Writes to /tmp work. Writes anywhere else fail with Read-only file system.
Note that --read-only does not affect volumes or bind mounts. Those keep the access mode you give them, so mount data volumes only where the application needs them, and add :ro where it does not.
docker run -d --read-only --tmpfs /tmp \
-v appdata:/var/lib/app \
-v ./config:/etc/app:ro \
myapp:1.0Finding what the app needs to write
Run the container read-only and watch for errors. Then use docker diff on a normal (writable) run to list the paths the application changed.
docker diff webEach line starts with A (added), C (changed), or D (deleted). Each path you see is a candidate for a tmpfs or volume.
Limiting resources
Resource limits use cgroups. Set them at run time.
| Flag | What it limits | Effect when exceeded |
|---|---|---|
| `--memory 256m` | RAM (and swap by default, see below) | The kernel's OOM killer stops a process |
| `--cpus 0.5` | CPU time, as half of one core | The container is throttled |
| `--pids-limit 100` | Number of processes and threads | `fork` fails, which stops fork bombs |
By default, setting --memory also allows the same amount of swap. To forbid swap, set --memory-swap equal to --memory.
docker run -d --memory 256m --memory-swap 256m myapp:1.0Common errors
- Container exits with code 137. The OOM killer ended it. Check with
docker inspect --format '{{.State.OOMKilled}}' web. Raise the limit only after confirming the application really needs more. - Application crashes on start with "Read-only file system". It writes somewhere unexpected, often
/var/run,/var/cache, or a log directory. Add a--tmpfsfor that path. - Limits set too low cause slow responses. CPU throttling shows up as latency, not errors. Measure with
docker statsbefore choosing values. - No limit at all. Without flags the container can use everything on the host. Treat unset limits as a defect.
Exercise
A container started with --read-only fails at startup with "Read-only file system" when writing to /var/run/app.pid. What is the safest fix, and why not remove --read-only?
Show answer
Add a tmpfs mount for that path, for example --tmpfs /var/run, so the application can write its PID file in memory while the rest of the root filesystem stays read-only. Removing --read-only would give up the protection against an attacker modifying binaries and files for the sake of one writable path.
Exercise
Which flag combination caps a container at 512 MB of memory with no swap, and which flag limits it to 200 processes?
Show answer
--memory 512m --memory-swap 512m caps memory with no swap, and --pids-limit 200 limits the process count.