Notes
What is in an image
Look at what an image contains. The base image: an operating system with a package manager, a shell, system libraries and utilities. Then your language runtime and dependencies. Then your code, often the smallest part. The consequence is that most of the vulnerabilities a scanner finds are in the base OS packages, not your code. And every extra tool in the image, a shell, a package manager, curl, is also a tool an attacker can use if they get in. Smaller images are safer on both counts.
- base image: an OS with package managers, shells, libraries, utilities
- your language runtime and dependencies
- your code: often the smallest part
- most CVEs live in the base OS packages, not your code
- every tool in the image is also a tool for an attacker
Scanning images
Image scanners inspect every layer, the OS packages and your application dependencies, matching them against vulnerability databases, like the dependency scanning from earlier but for the whole image. Trivy and Grype do this, along with Clair and Docker Scout. Good scanners also catch secrets baked into layers, flag misconfigurations, and produce an SBOM of the image. Run them at build time to gate the push, and in the registry, and rescan existing images continuously because new vulnerabilities appear daily.
- scan OS packages AND app dependencies in the image
- match against CVE databases
- tools: Trivy, Grype, Clair, Docker Scout
- also find: secrets baked in, misconfig, SBOM of the image
- run at build and in the registry; rescan continuously
Minimal base images
The single highest-impact move in container security is shrinking the base image. A full OS base like Ubuntu or Debian carries hundreds of packages, and many vulnerabilities. Slim variants trim that. Alpine is small. And distroless images go furthest: only your application and its runtime, no shell, no package manager. Fewer packages means dramatically fewer vulnerabilities, and no shell means an attacker who lands inside has almost nothing to work with. Minimal images win on every axis.
- full OS base (ubuntu, debian): hundreds of packages, many CVEs
- slim variants: fewer packages
- alpine: small, musl-based
- distroless: just your app and runtime; no shell, no package manager
- fewer packages = far fewer CVEs and less to attack
Harden the container
Beyond the base image, harden the container. Run as a non-root user, set with the USER instruction, so a container escape does not start as root. Use a read-only filesystem and drop Linux capabilities the app does not need. Never bake secrets into layers, because they persist in the layer history even if a later layer deletes them, like git. Pin the base image by its digest, not a moving tag. And use multi-stage builds so compilers and build tools stay out of the final image.
- run as a non-root user (USER in the Dockerfile)
- read-only filesystem; drop Linux capabilities
- never bake secrets into layers (they persist even if deleted later)
- pin base image by digest, not a moving tag
- multi-stage builds: no build tools in the final image
In the pipeline
Image security pulls together the whole part. You build on a minimal base with a multi-stage build and a non-root user. You generate an SBOM of the image. You scan it and block new critical vulnerabilities, focusing on the app-layer ones you can actually fix. You sign the image. At deploy, an admission controller verifies the signature and your policies. And you rescan registry images continuously. Every episode of this part shows up in that one flow, which is how modern container delivery is secured.
- build on a minimal base, multi-stage, non-root
- generate an SBOM (ep 5)
- scan the image; block new criticals in the app layer
- sign the image (ep 6)
- at deploy, admission verifies signature and policy (ep 11)
- rescan registry images continuously
Handling the findings
A word on image scan results, because it differs from code. For vulnerabilities in your application layer, update your dependencies; you control those. For vulnerabilities in the base operating system, you do not patch packages by hand; you update to a newer base image or switch to a smaller one, and rebuild. Often a vulnerability is in a base package your app never uses, and a minimal or distroless base removes it entirely. And prioritise by reachability and exposure, as the triage episode taught.
- app-layer CVEs: update your dependencies (you control these)
- base-OS CVEs: update or switch the base image, do not patch by hand
- unfixable base CVE in an unused package: minimal base removes it entirely
- prioritise by reachability and exposure, as always
Common mistakes
Mistakes. Shipping a full OS base when a minimal or distroless one would do, carrying hundreds of needless vulnerabilities. Running the container as root. Baking secrets into layers where they persist. Referencing the base image by a moving tag instead of a digest. Leaving build tools in the final image. Scanning once at build but never rescanning registry images. And patching base-OS packages by hand instead of rebuilding on an updated base.
- a full OS base when distroless would do
- running as root
- secrets baked into layers
- base image on a moving tag
- build tools left in the final image
- scanning at build, never rescanning the registry
- hand-patching base packages, not rebasing
Say it like this
In the interview: I start from a minimal or distroless base, because most image vulnerabilities come from the base OS, so shrinking it is the biggest win. I use a multi-stage build to keep build tools out, run as a non-root user, keep secrets out of layers, and pin the base by digest. I scan with Trivy or Grype at build and continuously in the registry, generate an SBOM, sign it, and verify at admission. For findings, I update dependencies for app-layer issues and rebase for base-OS ones.
- "Minimal or distroless base, multi-stage build, non-root
- user, no secrets in layers, base pinned by digest. Scan
- with Trivy or Grype at build
- and in the registry, generate
- an SBOM, sign it, and verify
- at admission. Most CVEs are in the base OS, so shrinking it
- is the biggest win."