Sub topic 15
Auditing with the CIS Docker Benchmark and monitoring at runtime
This chapter assumes you have read the earlier chapters on the daemon, runtime restrictions, and image scanning. You should be able to read a docker run command and edit /etc/docker/daemon.json. Hardening is only useful if you can check that it stayed in place and notice when something goes wrong. Auditing checks configuration. Monitoring watches behavior.
What the CIS Docker Benchmark is
The CIS Docker Benchmark is a published checklist from the Center for Internet Security. Each item has a number, a rationale, an audit command, and a remediation. Items are grouped into sections:
| Section | Covers |
|---|---|
| Host configuration | Separate partition for `/var/lib/docker`, auditing of Docker files |
| Daemon configuration | TLS, `userns-remap`, logging, `no-new-privileges` |
| Daemon files | Ownership and permissions of the socket and config files |
| Container images and build files | Non-root `USER`, no secrets in the image, trusted base images |
| Container runtime | Capabilities, read-only root filesystem, resource limits, no `--privileged` |
| Operations | Image sprawl, container sprawl, and similar housekeeping |
Many items repeat controls from earlier chapters. The benchmark gives you a way to confirm them across many hosts.
Running an automated audit
Docker Bench for Security is an open-source script that runs the benchmark checks against a host. It reports each check as PASS, WARN, INFO, or NOTE.
docker run --rm --net host --pid host --userns host --cap-add audit_control \
-e DOCKER_CONTENT_TRUST=$DOCKER_CONTENT_TRUST \
-v /etc:/etc:ro \
-v /lib/systemd/system:/lib/systemd/system:ro \
-v /usr/bin/containerd:/usr/bin/containerd:ro \
-v /usr/bin/runc:/usr/bin/runc:ro \
-v /usr/lib/systemd:/usr/lib/systemd:ro \
-v /var/lib:/var/lib:ro \
-v /var/run/docker.sock:/var/run/docker.sock:ro \
--label docker_bench_security \
docker/docker-bench-securityThis tool breaks several rules from this tutorial on purpose, such as host namespaces and a mounted socket. Run it on a trusted host, review the mounts first, and do not leave it running.
Treat the output as a list of questions, not a pass mark:
- Start with
WARNresults in the daemon and runtime sections. - Decide for each one whether to fix it, or to accept it and write down why.
- Re-run after changes and compare against the previous report.
A WARN can be a false alarm for your setup. For example, a check for a separate partition does not apply if your hosts are short-lived and rebuilt from images.
Monitoring at runtime
An audit is a snapshot. Containers also need watching while they run. Three sources are available without extra tooling.
Docker events show lifecycle activity as it happens:
docker events --filter type=container \
--filter event=start --filter event=die --filter event=oomResource use shows runaway processes, such as a cryptominer or a fork bomb that slipped past --pids-limit:
docker stats --no-stream --format "table {{.Name}}\t{{.CPUPerc}}\t{{.MemUsage}}\t{{.PIDs}}"Filesystem changes show what a container modified compared with its image:
docker diff webOn a container started with --read-only, docker diff should show almost nothing outside /tmp. A new file under /usr/bin is a strong sign of compromise.
For deeper visibility, use a runtime security tool such as Falco. It watches kernel system calls and raises alerts on rules like a shell starting inside a container, a write below /etc, or an unexpected outbound connection. Send alerts and Docker logs to a central system. Logs kept only on the host are lost if the host is compromised.
flowchart TDHow the pieces fit
Common errors
- Treating a clean benchmark run as proof of security. The benchmark checks configuration, not application bugs.
- Auditing once at setup and never again. Configuration drifts as people change hosts.
- Collecting logs without anyone reading the alerts.
- Running audit tools with broad mounts and forgetting to remove them afterward.
Exercise
A container was started with --read-only --tmpfs /tmp. Running docker diff on it shows A /usr/local/bin/xmrig. What does this suggest, and what do you do next?
Show answer
The root filesystem should be unwritable, so a new file in /usr/local/bin suggests the read-only setting was missing or bypassed, and that someone added a program (xmrig is a cryptocurrency miner). Check how the container was really started, preserve logs and the container for investigation, stop and replace it from a known-good image, and rotate any secrets it could reach.