Opscraft
Docker · Lesson 8 of 11 · 4 min read

Container security

Defence in depth

  1. Minimal base image — slim, Alpine, Chainguard/Wolfi, or distroless (no shell, no package manager). Fewer packages = fewer CVEs and fewer tools for an attacker.
  2. Run as non-root — USER 10001. Root in a container is root on the host if the container escapes; a non-root UID limits the damage. Rootless Docker/Podman go further by running the daemon itself unprivileged.
  3. Drop capabilities — --cap-drop ALL --cap-add NET_BIND_SERVICE as needed.
  4. No privilege escalation — --security-opt no-new-privileges.
  5. Read-only root filesystem with tmpfs where writes are needed.
  6. Never --privileged unless you truly need host devices — it disables almost all isolation (all capabilities, device access, no seccomp). Same caution for mounting /var/run/docker.sock.
  7. Seccomp/AppArmor — keep Docker's default profiles on.
  8. Resource limits so a compromised or buggy container can't DoS the host.
  9. Secrets at runtime from a secret store, never in the image.

The Kubernetes equivalents live in the pod securityContext: runAsNonRoot, runAsUser, readOnlyRootFilesystem, allowPrivilegeEscalation: false, capabilities.drop: ["ALL"], seccompProfile: RuntimeDefault — enforced cluster-wide by Pod Security Admission or OPA/Kyverno.

Enlarge diagramMinimal base imagedistroless, slim: fewer CVEs and toolsNon-root userUSER 10001Drop capabilities--cap-drop ALL, add back only what is neededNo privilege escalationno-new-privilegesRead-only root filesystemtmpfs for /tmpResource limitsmemory, CPU, PIDsNever --privilegedand never mount docker.sockSecrets at runtimefrom a secret store
Figure 1. Layers of container hardening. Each one limits what an attacker can do if the app is compromised.
Swipe sideways to see all of it, or tap to open it full screen.

Supply chain security

  • Scan images in CI (Trivy, Grype, Snyk) and in the registry (ECR basic or enhanced scanning via Inspector). Fail the pipeline on critical/high CVEs with a fix available, with an exception process.
  • SBOM — generate a software bill of materials (Syft, docker buildx build --sbom=true) so you can answer "are we affected by CVE-X?" in minutes.
  • Sign images (cosign/Sigstore or AWS Signer/Notation) and verify signatures at admission (Kyverno/Gatekeeper/Connaisseur) so only images built by your pipeline run.
  • Provenance attestations (SLSA) record how and where the image was built.
  • Base image hygiene — rebuild regularly to pick up patched bases even when app code hasn't changed.
Enlarge diagramBuildfrom a pinned,patched baseScanTrivy / Grype /InspectorSBOMlist of everythinginsideSigncosign or AWSSignerAdmitKyverno verifiesthe signature12345
Figure 2. A secure image pipeline: scan, describe, sign, and only admit signed images into the cluster.
Swipe sideways to see all of it, or tap to open it full screen.

Debugging minimal images

Distroless images have no shell, so docker exec sh fails. Options: a :debug variant of the base image, docker run --pid=container:<id> --network=container:<id> -it nicolaka/netshoot to share its namespaces, or in Kubernetes kubectl debug -it pod/<name> --image=busybox --target=<container> (ephemeral container).

Enlarge diagramSHARED NAMESPACESapp (distroless)no shell, no curlnetshoot / busyboxdig, curl, tcpdump, psThe tools container sees theapp's processes and networkwithout changing its image.docker run --network=container:app --pid=container:app -it netshootkubectl debug -it pod/app --image=busybox --target=app
Figure 3. Debugging a distroless container that has no shell: start a tools container that joins its network and process namespaces.
Swipe sideways to see all of it, or tap to open it full screen.