Read-Only Filesystems and tmpfs
What You'll Learn
You'll learn how to prevent container-level persistence attacks by running containers with immutable root filesystems and using temporary storage for ephemeral data.
The Core Idea
When you run a container with a read-only root filesystem, the container cannot write to its own filesystem layers. This makes it impossible for attackers to install malware, drop backdoors, or modify binaries that would persist across container restarts. It's like sealing a room so nothing can be left behind—any changes evaporate when the container stops.
However, many applications need some writable space for temporary files, caches, or runtime data. That's where tmpfs comes in: a memory-backed filesystem that provides writable storage without touching the disk. When the container stops, tmpfs disappears completely.
Why This Matters
Defense in depth against persistence: Even if an attacker compromises your container, they cannot establish a foothold that survives a restart. Modified binaries, dropped scripts, or configuration changes vanish.
Analogy: Think of a read-only filesystem like a whiteboard protected by glass—you can look but not write. tmpfs is like a chalkboard inside the glass case: you can write on it temporarily, but it gets wiped clean every time the case is closed.
Implementation Pattern
In Docker, you would typically:
- Set
--read-onlyflag to make the root filesystem immutable - Mount tmpfs volumes for directories that need write access (like
/tmp,/var/run, or application-specific cache directories)
This combination ensures containers remain stateless and attackers cannot leave traces behind.
Key Takeaway: Running containers with read-only filesystems blocks persistence mechanisms; use tmpfs for legitimate ephemeral storage needs while maintaining immutability guarantees.