How do you scan a Docker image with Trivy?
BeginnerHands-onTrivy
Answer
Short answer
Run trivy image <image>:<tag>. Trivy pulls (or reads) the image, detects the OS and language packages inside, matches them against its vulnerability database and prints a table of CVEs with severity, installed version and fixed version.
Examples
trivy image nginx:1.27
trivy image --severity HIGH,CRITICAL shop-api:1.4.0
trivy image --ignore-unfixed shop-api:1.4.0 # only CVEs that have a fix
trivy image --format json -o trivy.json shop-api:1.4.0
trivy image --input shop-api.tar # scan a saved image (docker save)
Output (shortened)
shop-api:1.4.0 (debian 12.6)
Total: 3 (HIGH: 2, CRITICAL: 1)
┌──────────────┬────────────────┬──────────┬───────────────────┬───────────────┐
│ Library │ Vulnerability │ Severity │ Installed Version │ Fixed Version │
├──────────────┼────────────────┼──────────┼───────────────────┼───────────────┤
│ libssl3 │ CVE-2024-XXXXX │ CRITICAL │ 3.0.13-1~deb12u1 │ 3.0.14-1~deb12u1 │
│ jackson-databind (app) │ CVE-2023-XXXXX │ HIGH │ 2.13.0 │ 2.13.4.2 │
Reading it
- OS packages (libssl3) → fix by updating the base image /
apt-get upgradein the Dockerfile. - App libraries (jackson) → fix by upgrading the dependency in pom.xml/package.json.
What to say in the interview
"trivy image scans OS packages and application libraries inside the image. I filter by HIGH/CRITICAL and often --ignore-unfixed in gates, then fix OS findings by updating the base image and library findings by bumping dependencies."
More on Trivy
What is Trivy and how do you scan an image with it?BeginnerWhat is a CVE and what do severity levels mean?BeginnerWhat is Trivy and what can it scan?BeginnerHow do you fail a CI build when Trivy finds HIGH or CRITICAL vulnerabilities?BeginnerWhat does trivy fs do?BeginnerWhat does trivy config scan?Beginner