Sub topic 11
Scanning Images and Reading SBOMs
This chapter assumes you can build and run images, know the four base image families covered earlier (Debian slim, Alpine, distroless, Docker Hardened Images), and know what a package manager is. You will scan an image for known vulnerabilities, generate a software bill of materials (SBOM), and learn to read both without being misled.
What a scanner and an SBOM each do
An SBOM is an inventory of the software inside an image: package names, versions, and where they came from. A vulnerability scanner takes that inventory and matches each name and version against databases of known vulnerabilities, each identified by a CVE ID such as CVE-2024-12345.
A scanner finds packages in three main places:
- The OS package database, such as the dpkg status file on Debian or the apk database on Alpine.
- Language lockfiles and manifests, such as
package-lock.jsonorrequirements.txtmetadata. - Metadata embedded in compiled binaries, such as Go build information.
Because the scanner reads the image, not your source, the base image you pick shapes the report.
Scanning to compare base images
Build the same application on each candidate base, then run the same scanner on each result.
docker image ls
docker scout cves myapp:latest
trivy image myapp:latestdocker image ls shows size. The other two commands list CVEs. To cut noise and fail a CI job on serious, fixable problems, add filters:
trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 myapp:latest--exit-code 1 makes the command return a failure status when it finds a match, which stops the pipeline.
Reading a result
Each finding has the same basic fields:
| Field | Meaning |
|---|---|
| Package | The library or tool that is affected |
| Installed version | What your image contains |
| Fixed version | The first version with a patch. Empty means no fix exists yet |
| Severity | The rating (LOW to CRITICAL), usually from CVSS |
| CVE ID | The identifier you can look up for details |
If a fixed version exists, the usual remedy is to rebuild on a newer base or upgrade the package. If none exists, you can only accept the risk, mitigate it, or switch to a base image that does not ship that package.
Common errors when interpreting scans
- Treating the CVE count as a score. A Debian slim image may list many low-severity findings that Debian has marked as minor or unfixed. A smaller number from another image is not automatically safer. Compare HIGH and CRITICAL findings that have fixes.
- Assuming scanners agree. Docker Scout and Trivy use different data sources and matching rules, so their counts differ. Pick one tool for your pipeline and compare images with that tool.
- Ignoring reachability. A vulnerable package that your application never calls is still reported. Vulnerability Exploitability eXchange (VEX) statements record that a CVE does not affect a given image. Some hardened image vendors publish them.
- Trusting an old result. New CVEs are published after your build. An image that scanned clean last month can fail today, so rescan images that are already deployed.
- Missing statically linked code. A distroless static image has almost no OS packages, so the report looks clean. Libraries compiled into your binary are visible only if the scanner can read the binary's embedded metadata.
Generating and reading an SBOM
Two formats dominate: SPDX and CycloneDX. Trivy and Syft can produce either.
trivy image --format spdx-json --output sbom.spdx.json myapp:latest
jq '.packages | length' sbom.spdx.json
jq -r '.packages[].name' sbom.spdx.json | sort | headThe first jq command counts the packages. The second lists their names. Run it on a slim image and on a distroless image and you will see how much of the OS the second one leaves out.
You can also have BuildKit attach an SBOM at build time, so it travels with the image in the registry:
docker buildx build --sbom=true --provenance=true -t myapp:latest .A stored SBOM lets you answer "which of our images contain this library?" without pulling and scanning every image again when a new CVE is announced.
Exercise
A scan of your Alpine-based image reports a HIGH severity CVE in a package, and the "Fixed version" column is empty. Name two options for responding.
Show answer
No patched version exists yet, so upgrading will not help. You can accept and document the risk (for example with a VEX statement if the code is not reachable), or remove the package or switch to a base image that does not include it, such as distroless. You can also rescan later, because a fix may be published.