Sub topic 8
Making the root filesystem read-only
This chapter assumes you can run containers with docker run, know basic Linux file permissions, and have seen the capability and non-root-user chapters. Here you will make the container's root filesystem read-only and see what breaks.
Why a read-only root filesystem helps
A container's root filesystem is normally a writable layer stacked on top of the image layers. Anything running inside can create or change files there. After a compromise, an attacker commonly uses that to drop a script, replace a binary, or edit a config file in /etc.
A read-only root filesystem removes that option. The kernel refuses every write to the image's paths with an error, whatever user is writing. Even root inside the container cannot change /etc or /usr.
This control also acts as a check on your application. If the app only works when it can write to its own install directory, you find that out early.
Turning it on in Docker
Add --read-only to docker run. This example also gives the container a small scratch area at /tmp and applies the controls from earlier chapters.
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"'Expected output:
tmp is writable
touch: cannot touch '/etc/blocked': Read-only file system
root filesystem is not writableThe first write succeeds because /tmp is a separate tmpfs mount. The second fails with Read-only file system. That message (error code EROFS) is the one to look for when something breaks later.
What still needs to be writable
Most programs write somewhere. Common places are:
| Path | Typical reason |
|---|---|
| `/tmp` | Temporary files, upload buffers |
| `/var/run` or `/run` | PID files and sockets |
| `/var/cache`, `/var/log` | Package caches, log files |
| Application data directory | Databases, uploads, state |
You can cover these with two kinds of mount:
- A tmpfs mount keeps data in memory and discards it when the container stops. Use it for scratch data.
- A volume keeps data outside the container's filesystem and can survive restarts. Use it for data you need to keep.
Mount only the specific paths the application needs, not whole directories like /var. Keep each writable area as small as you can.
Mounting a volume for persistent data
A named volume gives a path that stays writable while the rest stays locked.
docker volume create appdata
docker run --rm \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=64m \
-v appdata:/data \
alpine sh -c 'echo saved > /data/state.txt && cat /data/state.txt'Here /data accepts writes and is kept after the container exits. Everything else is read-only. If you run as a non-root user, the volume's directory must be owned by that user, or the write fails with Permission denied.
Finding what the app needs to write
When an application fails after you enable --read-only, read its error output first. Look for Read-only file system and the path next to it. Then add a tmpfs or volume for that path and run again.
You can also list files that a normal container has changed:
docker run -d --name probe nginx
sleep 5
docker diff probe
docker rm -f probedocker diff prints the files added (A), changed (C), or deleted (D) in the writable layer. Those paths are good candidates for tmpfs or volume mounts.
Common mistakes
- Mounting a tmpfs over a path that holds files from the image. The mount hides them, and the app can no longer find its files.
- Forgetting that tmpfs data disappears on restart.
- Making
/writable again with a broad volume such as-v /host/dir:/. - Setting the size too small, which produces
No space left on deviceerrors instead of read-only errors.
Exercise
A container started with --read-only fails with "nginx: [emerg] mkdir() "/var/cache/nginx/client_temp" failed (30: Read-only file system)". What is the smallest fix that keeps the rest of the filesystem read-only?
Show answer
Mount a writable path at the directory the error names, for example --tmpfs /var/cache/nginx. Nginx may also need a writable /var/run for its PID file, which you would add the same way after the next error. Do not remove --read-only or mount a broad directory such as /var.
Exercise
Why does a read-only root filesystem not protect data stored in a volume mounted at /data?
Show answer
A volume is a separate mount, and --read-only applies only to the container's root filesystem. The volume stays writable unless you mount it with :ro.