Container security
Containers package the application with its dependencies. Their security spans the image (what it contains), the build (how it’s built), and the runtime (how it runs). A misconfigured container is often the escape route to the host or the cluster. The key premise: a container is not a VM; it shares the host kernel, so isolation is weaker than it looks.
Threat model
Section titled “Threat model”The risk spreads across the three phases of the image lifecycle:
- Image: a base with known CVEs, unnecessary packages that widen the surface, secrets baked into a layer, an image from an unverified source.
- Build: a
Dockerfilethat copies the whole repo (including.gitand.env), secrets passed viaARGthat stay in history, unpinned dependencies. - Runtime: container running as
root, privileged, with the Docker socket mounted, excess capabilities, no resource limits → host escape.
flowchart LR
BASE["Base image"] --> BUILD["docker build"]
BUILD --> SCAN["Image scan (CVE + misconfig + secrets)"]
SCAN -->|clean| SIGN["Sign + SBOM"]
SIGN --> REG["Registry"]
REG --> VERIFY{"Admission verification"}
VERIFY -->|OK| RUN["Hardened runtime"]
VERIFY -->|fail| REJECT["Reject"]
RUN -. bad config .-> ESCAPE["Host / cluster escape"]
style ESCAPE fill:#c0392b,color:#fff
Minimal, reproducible image
Section titled “Minimal, reproducible image”Less image, less surface. The goal is an image containing only the app and the bare minimum to run it:
- Multi-stage build: compile in one stage and copy only the binary into a minimal final image.
- Minimal base:
distroless,alpine, orscratchinstead of a full distro. - Non-root user and read-only filesystem.
- Pin the base by digest, not a moving tag.
# Build stageFROM golang:1.23 AS buildWORKDIR /srcCOPY go.mod go.sum ./RUN go mod downloadCOPY . .RUN CGO_ENABLED=0 go build -o /app ./cmd/server
# Minimal final stage, no shell or package managerFROM gcr.io/distroless/static-debian12@sha256:<digest>COPY --from=build /app /appUSER 65532:65532ENTRYPOINT ["/app"]Image scanning
Section titled “Image scanning”Before publishing, scan the image for vulnerabilities, misconfigurations, and secrets:
# Trivy: OS and dependency CVEs + misconfig + secretstrivy image --severity HIGH,CRITICAL --scanners vuln,misconfig,secret myapp:1.4.2
# Inspect layers for secrets / files copied by mistakedocker history --no-trunc myapp:1.4.2Scanning integrates into the pipeline; together with signing and the SBOM, it closes the image’s chain of trust.
Hardened runtime
Section titled “Hardened runtime”Most escapes come from giving the container more than it needs. Minimal secure configuration:
# docker-compose / orchestrator equivalentservices: app: image: registry.example/myapp@sha256:<digest> user: "65532:65532" # non-root read_only: true # read-only root FS cap_drop: ["ALL"] # no capabilities security_opt: - "no-new-privileges:true" # no privilege escalation tmpfs: ["/tmp"] # NEVER: privileged: true, or mounting /var/run/docker.sockRuntime golden rules:
- Never
--privilegedor mount the Docker socket in an exposed container: it’s equivalent to granting root on the host. - Drop all capabilities and add back only the essential ones.
no-new-privilegesto cut escalations via SUID binaries.- CPU and memory limits to prevent exhaustion DoS.
- For strong isolation, consider runtime sandboxes (gVisor, Kata Containers).
Red Team
Section titled “Red Team”- Mine the image: extracting and reviewing layers (
docker save, unpack) surfaces secrets, keys, and source code copied by mistake (see secrets). - Hunt the runtime misconfig: a
privilegedcontainer, one withdocker.sockmounted, or one with capabilities likeSYS_ADMINis a near-direct host escape. - Base CVEs: an old base image drags in OS CVEs exploitable from inside the container.
- Pivot to host/cluster: from a compromised container, abuse mounts, the socket, or a vulnerable kernel to reach the node and from there the cluster.
Blue Team
Section titled “Blue Team”- Minimal image (distroless/scratch), multi-stage, base pinned by digest.
- Mandatory scanning for CVE, misconfig, and secrets before publishing.
- Signing + SBOM and admission verification (unsigned doesn’t run).
- Hardened runtime: non-root, read-only FS,
cap_drop ALL,no-new-privileges, no privileged, no Docker socket. - Resource limits always.
- Private registry with access control; don’t pull public images unverified.
- Runtime detection (Falco) for anomalous behavior inside the container.
Checklist
Section titled “Checklist”- Minimal base image, pinned by digest.
- Multi-stage build; only the artifact in the final image.
- Non-root user and read-only root FS.
- CVE + misconfig + secrets scanning in the pipeline.
- Image signing and SBOM; admission verification.
-
cap_drop: ALL,no-new-privileges, noprivileged. - No
docker.sockmounted in exposed containers. - Defined CPU/memory limits.
- Private registry with access control.
- Runtime behavior detection (e.g. Falco).