Container Security Essentials
Reduce the blast radius of containers by minimising images, dropping privileges and scanning for known vulnerabilities.
Intermediate · about 5 hours · difficulty 3/5
Learn first: Building Good Images, Users, Groups and Permissions
Containers improve isolation but are not a security boundary by default. A container running as root with extra capabilities can often break out to the host.
Build-time
- Minimal base images (distroless, slim, Alpine) mean fewer packages and fewer CVEs.
- Scan images with Trivy, Grype or registry scanners, and fail builds on critical findings that have fixes.
- Never bake secrets into layers; use BuildKit secret mounts for build-time credentials. Anything in a layer can be extracted, even if a later layer deletes it.
- Rebuild regularly so base-image patches land.
Run-time
- Run as a non-root user, with a read-only root filesystem where possible.
- Drop Linux capabilities (
--cap-drop ALL, add back only what is needed), never use--privilegedcasually, and never mount the Docker socket into untrusted containers. - Keep the default seccomp and AppArmor/SELinux profiles on.
- Rootless Docker or Podman reduces host risk further.
- Set memory and CPU limits so one container cannot starve the host.
Pitfalls
Scanning once and never again, ignoring scan output because there is too much noise (triage by fixable and exploitable), and trusting random public images.
Good enough to move on: your images run as non-root with a read-only filesystem and no extra capabilities, and CI fails on fixable critical vulnerabilities.
Key concepts
- Minimal base images
- Image scanning (Trivy, Grype)
- Non-root and read-only rootfs
- Linux capabilities
- Build secrets
- seccomp and AppArmor
- Rootless containers
Mini project
Scan one of your images with Trivy, fix the findings you can by changing base image and packages, then run it with --read-only, --cap-drop ALL and a non-root user and document what had to change.
Check yourself
Why is mounting /var/run/docker.sock into a container roughly equivalent to giving it root on the host?
Free resources
- Docker Engine security (Docker)
- Trivy (Aqua Security)
- OWASP Docker Security Cheat Sheet (OWASP)