Skip to content

Docker audit fix · Container Hardening

How to Configure Read-Only Root Filesystems in Docker

Docker gives every container a writable root filesystem unless it is started with --read-only, so anything that gains code execution inside can overwrite binaries and persist there. Turn it on and mount tmpfs for the paths the app genuinely writes.

Hiren KalariyaHiren KalariyaLast reviewed read-only-filesystem
CtrlOpsRead-Only FilesystemLOW

What did your audit say?

WARN · when the audit printed

Some have a writable root fs

The containers named have a writable root filesystem, so a compromised process can overwrite binaries and persist inside them.

Severity
Low
Needs root or sudo
Yes
Results it can return
WARN · SKIP · PASS
Check in this audit
5 of 5

Every threshold on this page is transcribed from the docker-security-container-hardening audit script that CtrlOps runs, and a build check fails if the two ever disagree. See all 5 Container Hardening checks, or how the audit runs.

The check inspects one boolean, .HostConfig.ReadonlyRootfs, on every container returned by docker ps -q, using a template that emits the name of any container where that field is false. Those names are then counted. A count of zero is a PASS; anything above zero is a WARN that reports N of M containers with a writable root filesystem and names up to five of them.

Needs a reachable daemon and at least one running container; otherwise SKIP. It reads .HostConfig.ReadonlyRootfs for each running container - a single boolean, set by --read-only or Compose read_only: true. It says nothing about mounted volumes: a container with a read-only root filesystem and a writable bind mount still passes, which is correct (that is the intended pattern) but means PASS does not imply "nothing is writable". Severity is LOW because this is a tampering-resistance measure, not a containment boundary.

What each result means
ResultWhenWhat it means
SKIPNo running containersNothing was verified: no filesystem setting was read, so treat every container as unchecked.
PASSAll use --read-onlyNo running container can write to its own root filesystem, though mounted volumes may still be writable.
WARNSome have a writable root fsThe containers named have a writable root filesystem, so a compromised process can overwrite binaries and persist inside them.

A read-only root filesystem stops an attacker who gains code execution from dropping tools, modifying binaries, or persisting inside the container. Writable scratch space is provided explicitly with --tmpfs or named volumes. OWASP Docker Cheat Sheet Rule #8 "Set filesystem and volumes to read-only"; CIS Docker 5.12 "Ensure that the container's root filesystem is mounted as read only"; NIST SP 800-190 4.4.

  • Most images expect to write to /tmp, /var/run, /var/cache, log directories, or PID files. Turning on --read-only without the matching --tmpfs mounts causes startup failures ("Read-only file system").
  • Advisory (LOW). Expect a WARN on typical deployments; it is a hardening target, not a bug.
  • A near-duplicate check exists (Read-Only Volume Enforcement, MEDIUM) that evaluates all containers, not just running ones; one fix satisfies both.
  1. Find out what the container writes

    Run this against a working copy started without --read-only. Every A or C line is a path that needs a writable mount.

    sh
    docker diff <name>

  2. Turn on --read-only with writable mounts for those paths

    sh
    docker run --read-only --tmpfs /tmp --tmpfs /run -v data:/var/lib/app ...

    In Compose:

    yaml
    read_only: truetmpfs:  - /tmp  - /run

    Use a tmpfs for scratch space that can vanish on restart, and a named volume for anything the app must keep.

  3. Recreate and watch the logs

    sh
    docker compose up -d --force-recreatedocker logs <name> 2>&1 | grep -iE 'read-only file system|EROFS'

    Each hit is a path you missed. Add a tmpfs or volume for it and recreate again until the logs are clean.

sh
docker inspect -f '{{.Name}} readonly={{.HostConfig.ReadonlyRootfs}}' $(docker ps -q)# expected: true# Prove it from inside:docker exec <name> sh -c 'touch /test-write' 2>&1# expected: Read-only file system# And that the scratch space the app needs is still writable:docker exec <name> sh -c 'touch /tmp/ok && echo tmpfs writable'docker inspect -f '{{.HostConfig.Tmpfs}} {{range .Mounts}}{{.Destination}}:{{.RW}} {{end}}' <name>

Debugging

  • This one message covers two different cases: the audit could not reach the Docker daemon, or it reached it and nothing was running. Run docker ps as the audit user, then sudo docker ps. If both fail, the daemon is unreachable: check systemctl status docker, then run the audit as root or give the account sudo. If they work but list nothing, there is genuinely nothing to evaluate, and a stopped container is never checked. The Privileged Containers check reports the two cases as separate messages, so its result tells you which one you hit.
  • Truncated with head -5; the N of M count is complete. Full list: docker inspect -f '{{if not .HostConfig.ReadonlyRootfs}}{{.Name}}{{end}}' $(docker ps -q).
  • It needs somewhere to write. Add --tmpfs /tmp --tmpfs /run (Compose tmpfs:), or a named volume for genuine state. Find out what it writes with docker diff <name> on a running copy without --read-only - every A/C line is a path that needs a writable mount.
  • The container needs recreating for a HostConfig change; docker compose up -d --force-recreate.
  • The root filesystem is read-only, mounted volumes are not. Check .Mounts with the command above and mount read-only wherever the container does not need to write; see the Writable Volume Mounts check.

Sources

Next

Re-run the Container Hardening audit after applying the fix and confirm this check moves to PASS. CtrlOps runs all 5 checks over your existing SSH connection and scores the result, so the change is visible without reading another config file.

Download for MacDownload for WindowsAll 5 Container Hardening fixes

14-day free trial, no credit card required.

Explore article with AI tools

Written by

Hiren Kalariya

Co-Founder & CEO, TST Technology

Hiren Kalariya is the Co-Founder and CEO of TST Technology and the product lead behind CtrlOps, a local-first desktop app that makes Linux server management fast and easy for developers and DevOps teams. Over the past five years he has shipped 60+ production systems, and he still tests every tool he writes about on real servers.

Stop juggling tools. Start managing fleets.

PuTTY
Termius
MobaXterm
WinSCP
SecureCRT
Royal TS
Warp
CtrlOps14 tools left
Webmin
aaPanel
RunCloud
Dokploy
IP spreadsheet
bash (2)
sticky notes

One app. All your servers. No context switching. Free to start, free to leave.

Start instantly · No credit card · Cancel anytime

Your privacy preferences. We use product analytics to improve CtrlOps. Google Analytics cookies and live chat stay off until you allow them. Cookie policy