Skip to content

Lesson 9 of 14 · 20m

Container Hardening Past the Defaults

Digests over tags, cap_drop ALL, read_only roots, and what xz and Docker Hub actually taught about running strangers' binaries.

docker pull is running a stranger's binary as root on your machine — with a namespace bow on it. Docker Hub is a publishing platform, not a curator: a 2024 JFrog scan found roughly one in five repositories on it hosting malicious content — typosquats, cryptominers, credential stealers, fake "official" images. The supply chain is the threat model most self-hosters skip.

What xz taught everyone

In March 2024, CVE-2024-3094 surfaced: a pseudonymous contributor spent years earning commit rights on xz-utils, then shipped release tarballs (5.6.0, 5.6.1) whose "test files" were a backdoor that hooked into sshd through the libsystemd→liblzma chain — targeting the RSA key-verification path. Found by accident: Postgres developer Andres Freund benchmarked sshd logins running ~500ms slow. Two lessons that transfer to containers: release artifacts can differ from the repo you read, and a multi-year social campaign is a realistic attack on infrastructure software. Your image's maintainer identity is part of your security posture.

Pin by digest, not by tag

image: postgres:17.2 is a mutable pointer — the registry re-publishes the tag on every rebuild, and pull this month gets different bytes than last month. For real immutability, pin the digest:

image: postgres:17.2@sha256:f3c4a9...

(docker inspect or the registry page shows it.) Tags for readability, digests for proof. The tradeoff: you stop getting quiet security rebuilds under the same tag — so a pinned digest pairs with a habit of actually bumping versions, not fossilizing.

Runtime hardening in compose

services:
  app:
    image: myapp:1.4.2@sha256:...
    user: "1000:1000"                 # or the image's own non-root user
    read_only: true
    tmpfs: [/tmp]
    cap_drop: [ALL]
    # cap_add: [NET_BIND_SERVICE]     # add back surgically, if needed
    security_opt: ["no-new-privileges:true"]
    pids_limit: 512
    mem_limit: 512m

Each knob's job: user: runs the process unprivileged — many official images ship a dedicated user already (node, postgres); read the Dockerfile and use it. cap_drop: [ALL] strips every Linux capability — behind a reverse proxy, even NET_BIND_SERVICE is unnecessary since ports are >1024. no-new-privileges blocks setuid escalation inside the container. read_only: makes the root filesystem immutable — the app gets writable space only where you mount it (tmpfs for scratch, named volumes for data). pids_limit/mem_limit stop a fork bomb or leak from eating the host.

The honest tradeoffs: read_only breaks apps that write to their install tree (fix by mounting the paths they legitimately need — don't just flip it off); dropped capabilities break the rare image that needs one (add back exactly the named cap); and user: can collide with baked-in file ownership (/app owned by root, app can't write) — solve with a volume or an entrypoint fix, not a revert to root.

Scan what you run

docker scout cves myapp:1.4.2 and trivy image myapp:1.4.2 both enumerate known CVEs in an image's packages — a cheap signal before you adopt an image and a reasonable weekly skim after. A clean scan today means nothing next month; scanners report known vulnerabilities, which is a growing list applied to a frozen filesystem. The response to a growing CVE list is your update cadence, not despair — it's the reason compose pull belongs in the weekly loop.

Field notes

  • Prefer "official" namespaces (docker.io/library/*, verified publishers) — the namespace convention isn't an audit, but it beats a stranger's repackage.
  • Never -v /:/host or mount the docker socket without understanding it's root; read every volume line as a grant.
  • Rootless Docker or userns-remap is the deeper fix for "container root = host root"; it's worth it when you run genuinely untrusted workloads, and it's overkill for a stack of trusted images behind a proxy.
  • docker run --rm -it image sh on a candidate image, before it joins compose, is five minutes of inspection — check id, the filesystem, what it expects to write — that pays for itself in avoided surprises.

Was this lesson helpful?

Sign in to rate

Keep reading

Related articles from the SudoSecurity blog.