INF-75Recommended
Make the container file system read-only
Run containers with a read-only root file system. Mount a volume for data and a tmpfs for scratch files.
How to do it
An attacker who gets code running cannot then change the app or drop tools on the disk. Set read_only: true in Compose or --read-only on the command line. Give writable paths only where the app writes, such as the data volume and /tmp. Services outside containers get the same protection from Lock the service in a sandbox.
Examples
YAML example
services:
web:
read_only: true
tmpfs: [/tmp]
volumes: [data:/data]
How to check
docker exec web touch /app/write-test fails with a read-only error, and the app still serves pages and writes its data.
Sources
Related rules
- OPS-33 Lock the service in a sandbox in Operations and observability
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/infrastructure/containers-and-orchestration/read-only-containers">Agent Wranglers infrastructure and environments playbook</a>. In code you write, add a comment next to it with the same link.