Skip to content
Pwnversity
DSO-CONTAINERSintermediate · 6 min · upd. 2026-10

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.

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 Dockerfile that copies the whole repo (including .git and .env), secrets passed via ARG that 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

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, or scratch instead of a full distro.
  • Non-root user and read-only filesystem.
  • Pin the base by digest, not a moving tag.
# Build stage
FROM golang:1.23 AS build
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app ./cmd/server
# Minimal final stage, no shell or package manager
FROM gcr.io/distroless/static-debian12@sha256:<digest>
COPY --from=build /app /app
USER 65532:65532
ENTRYPOINT ["/app"]

Before publishing, scan the image for vulnerabilities, misconfigurations, and secrets:

Ventana de terminal
# Trivy: OS and dependency CVEs + misconfig + secrets
trivy image --severity HIGH,CRITICAL --scanners vuln,misconfig,secret myapp:1.4.2
# Inspect layers for secrets / files copied by mistake
docker history --no-trunc myapp:1.4.2

Scanning integrates into the pipeline; together with signing and the SBOM, it closes the image’s chain of trust.

Most escapes come from giving the container more than it needs. Minimal secure configuration:

# docker-compose / orchestrator equivalent
services:
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.sock

Runtime golden rules:

  • Never --privileged or 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-privileges to cut escalations via SUID binaries.
  • CPU and memory limits to prevent exhaustion DoS.
  • For strong isolation, consider runtime sandboxes (gVisor, Kata Containers).
  • 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 privileged container, one with docker.sock mounted, or one with capabilities like SYS_ADMIN is 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.
  • 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.
  • 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, no privileged.
  • No docker.sock mounted in exposed containers.
  • Defined CPU/memory limits.
  • Private registry with access control.
  • Runtime behavior detection (e.g. Falco).