Lesson 9 of 9 · 28m
Runtime Hardening and Operational Guardrails
Run containers as non-root with dropped capabilities, read-only filesystems, explicit resource limits, controlled secrets, health checks, and observable failure behavior.
The hardening options people copy into Compose files often look impressive and fail the first time the application writes a temp file. Runtime hardening is not a pile of flags. It is a negotiated contract between application behavior and containment: the app gets the minimum resources and permissions it honestly needs, and the platform gets predictable failure when the process misbehaves.
Start with the baseline
A defensible baseline for a simple service looks like this:
services:
api:
image: registry.internal/app@sha256:1c4eefc0ffee
user: "10001:10001"
read_only: true
tmpfs:
- /tmp:rw,noexec,nosuid,size=64m
- /run:rw,noexec,nosuid,size=16m
cap_drop:
- ALL
security_opt:
- no-new-privileges:true
mem_limit: 512m
cpus: 1.0
pids_limit: 200
restart: unless-stoppedEach line answers a concrete question. Who is the process? Can it write the image filesystem? Can it gain new privileges? Can it exhaust CPU, memory, or process table capacity? If a line is present because a blog post said so, remove it until you understand it.
Non-root is necessary but insufficient
USER 10001 in the image or user: at runtime prevents accidental host-root identity in the common case. It does not stop network abuse, credential theft available to the process, writable volume corruption, or exploitation of application bugs. Combine it with filesystem and capability constraints.
Test startup and write paths:
docker compose up -d api
docker compose exec api id
docker compose exec api sh -c 'touch /tmp/write-ok && echo tmp ok'
docker compose exec api sh -c 'touch /app/write-fail' || echo 'root filesystem rejected write'A failure writing /app is success if the application never needed it. A failure writing a cache directory means the mount design is wrong, not that hardening should be disabled.
Secrets are runtime inputs, not image artifacts
Do not bake secrets into layers or pass long-lived secrets casually through environment inspection surfaces. Prefer runtime-mounted files with tight ownership:
secrets:
db_password:
file: ./secrets/db_password
services:
api:
secrets:
- db_passwordInside the container the file commonly appears under /run/secrets/db_password. In production platforms, use the orchestrator or secret manager integration. Remember that any process with the same privileges as the application can read what the application can read; secret management limits accidental exposure, not compromise after code execution.
Health checks must test real readiness
A health check that returns 200 while dependencies are broken is decorative. Check the thing users need:
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s CMD ["/server", "healthcheck"]For shellless images, implement a subcommand or use orchestrator probes rather than adding a shell merely for health checks. Decide what a failed check means: restart, temporary unavailability, or dependency degradation. Restarts can convert dependency outages into crash loops and amplify load.
Logging and observability are containment tools
A hardened container you cannot observe is operationally fragile. Emit structured logs to stdout/stderr, include build revision and request identity, and expose metrics where appropriate:
{"level":"info","ts":"2026-11-30T12:04:18Z","msg":"server ready","revision":"9f2c4a1","port":8080}Do not log secrets, session tokens, full credentials, or sensitive payloads because debugging was convenient. Runtime hardening includes preventing your observability pipeline from becoming a secret archive.
Test failure behavior before production does
Run controlled failure drills:
docker update --memory=128m api
docker compose restart api
docker compose logs --tail=100 api
docker inspect api --format '{{.State.OOMKilled}} {{.State.ExitCode}}'What happens when memory is exceeded, the database disappears, the secret file is absent, /tmp fills, or the process tries to write a read-only path? You want explicit errors and bounded blast radius, not undefined behavior hidden behind restarts.
Field notes
no-new-privilegesblocks privilege escalation through setuid/file-capability execution; it is cheap and usually correct.read_onlycontainers still need writable application scratch space; design it with tmpfs or volumes.- Restart policies are not availability architecture. They hide crashes unless paired with limits, probes, backoff, and alerting.
- Environment variables are visible to the process and often to runtime inspection; do not treat them as a secrets boundary.
- The best hardening diff is small, explainable, and tested against the real workload.
Was this lesson helpful?