Docker · Lesson 8 of 11 · 4 min read
Container security
Defence in depth
- Minimal base image — slim, Alpine, Chainguard/Wolfi, or distroless (no shell, no package manager). Fewer packages = fewer CVEs and fewer tools for an attacker.
- 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. - Drop capabilities —
--cap-drop ALL --cap-add NET_BIND_SERVICEas needed. - No privilege escalation —
--security-opt no-new-privileges. - Read-only root filesystem with tmpfs where writes are needed.
- Never
--privilegedunless you truly need host devices — it disables almost all isolation (all capabilities, device access, no seccomp). Same caution for mounting/var/run/docker.sock. - Seccomp/AppArmor — keep Docker's default profiles on.
- Resource limits so a compromised or buggy container can't DoS the host.
- 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 diagram
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 diagram
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 diagram
Swipe sideways to see all of it, or tap to open it full screen.