Read-Only Root Filesystem
What you'll learn: How to lock down containers by making their root filesystems read-only, preventing runtime modifications and strengthening immutability.
Why Make Filesystems Read-Only?
By default, containers can write to their entire filesystem. This creates security risks: malicious code could modify binaries, plant backdoor scripts, or alter configuration files during runtime. A read-only root filesystem prevents any writes to the container's base filesystem layer, ensuring the container runs exactly as built—nothing more, nothing less.
Think of it like shrink-wrapping a product: once sealed, you know nobody has tampered with the contents during transport.
Enabling Read-Only Root Filesystem
You configure this in the container security context using the readOnlyRootFilesystem field:
apiVersion: v1
kind: Pod
metadata:
name: secure-app
spec:
containers:
- name: app
image: nginx:1.21
securityContext:
readOnlyRootFilesystem: true
Now the container cannot write to /, /usr, /bin, or any standard directories.
Handling Writable Needs
Many applications need some writable space (logs, temp files, caches). You solve this by mounting emptyDir volumes to specific paths:
volumeMounts:
- name: tmp
mountPath: /tmp
- name: cache
mountPath: /var/cache/nginx
volumes:
- name: tmp
emptyDir: {}
- name: cache
emptyDir: {}
The root filesystem stays read-only; only these explicitly mounted volumes allow writes.
Security Benefits
- Prevents runtime tampering with binaries or scripts
- Limits attacker persistence mechanisms
- Enforces container immutability principles
- Reduces attack surface by blocking unauthorized modifications
Key Takeaway: Enabling readOnlyRootFilesystem: true in container security contexts prevents all writes to the container's base filesystem, forcing you to explicitly declare writable locations through volume mounts—dramatically improving security and enforcing immutable infrastructure practices.