Sub topic 2
Choosing a Better Base Image with Docker Scout and Trivy
Assume you can pull and run Docker images, read a Dockerfile, and use a command-line shell. This chapter focuses on choosing a base image, comparing image versions, and scanning images with Trivy.
Choosing a Better Base Image
A base image provides the operating system files and runtime used by your application image. It also supplies packages that scanners check for known vulnerabilities.
Docker Scout can suggest safer base images or updates:
docker scout recommendations nginx:latestReview each recommendation before applying it. A smaller or newer image may remove vulnerabilities, but it can also change operating-system packages, certificates, shells, or runtime behavior. Test the resulting image with your application.
Comparing Image Versions
Use Docker Scout to compare a new image with a known baseline:
docker scout compare --to myapp:1.0 myapp:2.0This comparison helps identify vulnerabilities added or removed between versions. It is more useful than looking only at the total count because a new image may fix several findings while introducing one serious issue.
Getting Started with Trivy
Trivy is an image scanner that examines operating-system packages and supported language dependencies. Install it using the package method for your operating system, then verify that the command is available:
trivy --versionTrivy uses vulnerability databases and advisory metadata. Results can differ from Docker Scout because the tools may use different databases, package detection logic, or database update times.
Scanning an Image with trivy image
Run an initial scan with:
trivy image python:3.12-slimThe report identifies the package, vulnerability ID, severity, installed version, and fixed version when one is available. A missing fixed version means the database does not currently identify an upgrade that resolves the finding.
For a focused release check, show only high and critical findings and hide findings without known fixes:
trivy image --severity HIGH,CRITICAL --ignore-unfixed python:3.12-slimUse the focused command for triage, but retain broader scans for awareness. Filtering can hide important medium findings that later become serious when new exploit information appears.
Exercise
Why should you compare image versions instead of relying only on the latest vulnerability count?
Show answer
Comparison shows which findings were added or removed, so an overall count cannot hide a newly introduced serious vulnerability.