SECURITY ENGINEERING · PART 9 · DEVSECOPS AND SUPPLY CHAIN · CONTAINERS · 134 OF 229

Container Image Scanning and Minimal Base Images

A container image bundles your code with an entire operating system, and most of its vulnerabilities come from that base, not your code. This episode covers container image security: what is in an image, scanning with Trivy and Grype, why minimal and distroless base images cut vulnerabilities dramatically, running as non-root, not baking in secrets, and scanning at build and in the registry.

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 9
  9. 10
  10. 11
  11. 12
  12. 13
  13. 14
  14. 15
  15. 16
  16. 17
  17. 18
  18. 19
  19. 20
  20. 21
  21. 22
  22. 23
  23. 24
  24. 25
  25. 26
  26. 27
  27. 28
  28. 29
  29. 30
  30. 31
  31. 32
  32. 33
  33. 34
  34. 35
  35. 36
  36. 37
  37. 38
  38. 39
  39. 40
  40. 41
  41. 42
  42. 43
  43. 44
  44. 45
  45. 46
  46. 47
  47. 48
  48. 49
  49. 50
  50. 51
  51. 52
  52. 53
  53. 54
  54. 55
  55. 56
  56. 57
  57. 58
  58. 59
  59. 60
  60. 61
  61. 62
  62. 63
  63. 64
  64. 106
  65. 107
  66. 108
  67. 109
  68. 110
  69. 111
  70. 112
  71. 113
  72. 114
  73. 115
  74. 116
  75. 117
  76. 118
  77. 119
  78. 120
  79. 121
  80. 122
  81. 123
  82. 124
  83. 125
  84. 126
  85. 127
  86. 128
  87. 129
  88. 130
  89. 131
  90. 132
  91. 133
  92. 134
  93. 135
  94. 136
  95. 137
  96. 138
  97. 139
  98. 140
  99. 141
  100. 142
  101. 143
  102. 144
1 / 10

Press play to hear the narration, or step through at your own pace.

Key takeaways

  • Most image CVEs are in the base OS
  • scan with Trivy/Grype
  • minimal and distroless bases win most
  • non-root, pin by digest

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.

Layers of risk
  • 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.

Image scanners
  • 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.

Smaller base, fewer CVEs
  • 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.

Build and run safely
  • 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.

Pulling it together
  • 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.

What to do with results
  • 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.

Avoid these
  • 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.

In the interview
  • "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."

Interview questions

Q1Why do most container image vulnerabilities come from the base image?

Because the base image bundles an entire operating system, its package manager, shell, libraries and utilities, which contain far more code than your application. Most CVEs a scanner reports are in those OS packages rather than in your own code.

Q2What do container image scanners do?

They inspect all layers of an image, matching OS packages and application dependencies against vulnerability databases, and often also detect baked-in secrets, misconfigurations and generate an SBOM. Trivy, Grype, Clair and Docker Scout are common tools.

Q3What is a distroless image?

A minimal container image containing only the application and its runtime, with no shell, package manager or general OS utilities. This dramatically reduces the number of vulnerabilities and leaves an attacker who gets inside with almost no tools to use.

Q4Why run containers as a non-root user?

So that a vulnerability or container escape does not immediately grant root privileges. Setting a non-root USER in the Dockerfile, dropping Linux capabilities and using a read-only filesystem reduce what an attacker can do inside and outside the container.

Q5Why should you not bake secrets into container layers?

Because layers are immutable and retained in the image history, so a secret added in one layer persists even if a later layer deletes it, and anyone who pulls the image can extract it. Secrets should be injected at runtime from a secret manager instead.

Q6Why pin a base image by digest instead of a tag?

Because tags are mutable and can be updated to point at a different, possibly malicious or vulnerable image. Pinning to an immutable digest guarantees you build on exactly the image you reviewed, improving reproducibility and supply-chain integrity.

Q7How do you remediate vulnerabilities found in an image?

For application-layer dependencies, update them. For base operating-system packages, update to a newer base image or switch to a smaller one and rebuild, rather than patching packages by hand. A minimal or distroless base often removes unused vulnerable packages entirely.

Q8What is a multi-stage build and why is it more secure?

A Dockerfile pattern that builds the application in one stage with compilers and tools, then copies only the resulting artifact into a clean, minimal final image. It keeps build tools out of the shipped image, reducing size and attack surface.