Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Scanning container images with Trivy

After this lesson: I can scan a container image and a project with Trivy and read what it found.

60 min lesson14 commandsVulnerability management lab: Trivy, CVSS, EPSS and KEV

A quick check that you are not a bot, then your own lab opens on the interactive lesson.

Already have an account? Sign in

What is inside an image you inherit?

Scanning a container image means listing every package inside it and checking each version against known vulnerabilities. Trivy, an open-source scanner, does this in seconds.

Most of an image is somebody else's work: a base system, libraries and tools. A scan is how you find out what came with it.

By the end of this lesson

Note

I can scan a container image and a project with Trivy and read what it found.

Key words

WordWhat it meansExample
Image scannera tool that lists an image's packages and their known flaws
Package databasethe file where a system records what is installed
Lock filea file pinning the exact versions a project usesrequirements.txt
Fixed versionthe first version without the flaw
End of lifeno more security fixes from the maker

The cells students fill in are left blank in this preview.

Starter

Have a go

Starter

4 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

A scanner reads the package database

An image scanner does not run the image or attack it. It reads files. First it reads which system the image is, such as Alpine 3.18.6. Then it reads the package database, which names every installed package and version.

Last it looks each one up in advisories for that system. The running web server holds the same files, so you can read them yourself.

Which Alpine is the web server?

Command
docker exec web cat /etc/alpine-release

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Read libssl3 from the package database

Command
docker exec web grep -A1 -x P:libssl3 /lib/apk/db/installed

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What the web server's files say

FileLineMeaning
/etc/alpine-release3.18.6The system and its release
/lib/apk/db/installedP:libssl3A package called libssl3
/lib/apk/db/installedV:3.1.4-r5Its installed version

The cells students fill in are left blank in this preview.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Your turn: read libexpat's version

Command
docker exec web grep -A1 -x P:libexpat /lib/apk/db/installed

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Findings, CVEs and packages differ

Trivy reports a finding for each package and CVE. One OpenSSL flaw appears twice, once in libssl3 and once in libcrypto3, because both packages come from it. So count CVEs and packages too.

Trivy also says when a system has reached its end of life. Alpine 3.18 gets no more fixes, so a fixed version may only exist in a newer release.

Try it: findings against CVEs

Code: python
findings = [
    ["libcrypto3", "CVE-2024-6119"],
    ["libssl3", "CVE-2024-6119"],
    ["curl", "CVE-2024-2398"],
    ["libcurl", "CVE-2024-2398"],
    ["libexpat", "CVE-2024-45492"],
]
cves = []
for f in findings:
    if f[1] not in cves:
        cves.append(f[1])
print(len(findings), "findings", len(cves), "CVEs")
# Count the packages instead: how many different ones are there?

Make a free account to save your work and track your progress.

Write the helper that reads reports, part 1

Command
docker exec tools sh -c "printf '%s\n' 'import csv' 'import gzip' 'import json' '' 'EPSS_FILE = \"/work/snapshot/epss-2026-11-20.csv.gz\"' 'KEV_FILE = \"/work/snapshot/kev-2026-11-20.json\"' '' 'def findings(name):' '    with open(\"/work/reports/\" + name + \".json\") as f:' '        report = json.load(f)' '    rows = []' '    for result in report.get(\"Results\", []):' '        rows = rows + (result.get(\"Vulnerabilities\") or [])' '    return rows' '' 'def cve_list(rows):' '    cves = []' '    for v in rows:' '        if v[\"VulnerabilityID\"] not in cves:' '            cves.append(v[\"VulnerabilityID\"])' > steps/snap.py"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the helper that reads reports, part 2

Command
docker exec tools sh -c "printf '%s\n' '    return cves' '' 'def epss(wanted):' '    scores = {}' '    with gzip.open(EPSS_FILE, \"rt\") as f:' '        for row in csv.reader(f):' '            if row[0] in wanted:' '                scores[row[0]] = [float(row[1]), float(row[2])]' '    return scores' '' 'def kev():' '    with open(KEV_FILE) as f:' '        catalogue = json.load(f)' '    return {v[\"cveID\"]: v for v in catalogue[\"vulnerabilities\"]}' '' 'def nvd(v):' '    return v.get(\"CVSS\", {}).get(\"nvd\", {}).get(\"V3Score\")' >> steps/snap.py"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Write the script that counts a report

Command
docker exec tools sh -c "printf '%s\n' 'import sys' 'import snap' 'import json' '' 'with open(\"/work/reports/\" + sys.argv[1] + \".json\") as f:' '    os = json.load(f).get(\"Metadata\", {}).get(\"OS\")' 'if os:' '    print(os[\"Family\"], os[\"Name\"], \"end of life:\", os.get(\"EOSL\", False))' 'rows = snap.findings(sys.argv[1])' 'packages = []' 'fixable = 0' 'for v in rows:' '    if v[\"PkgName\"] not in packages:' '        packages.append(v[\"PkgName\"])' '    if v.get(\"FixedVersion\"):' '        fixable = fixable + 1' 'print(\"findings\", len(rows), \"CVE\", len(snap.cve_list(rows)), \"packages\", len(packages), \"with a fix\", fixable)' > steps/count.py"

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the old image to JSON

Command
docker exec scanner trivy image --input /targets/web-1.25.3.tar --format json -o /work/reports/web.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Scan the new image to JSON

Command
docker exec scanner trivy image --input /targets/web-1.31.9.tar --format json -o /work/reports/web-new.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count the old image

Command
docker exec tools python3 steps/count.py web

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count the new image

Command
docker exec tools python3 steps/count.py web-new

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

What count.py printed for the two images

ImageAlpineEnd of lifeFindingsCVEsPackagesWith a fix
nginx 1.25.33.18.6True74441374
nginx 1.31.93.24.4False127412

The cells students fill in are left blank in this preview.

A common mistake

Warning

Many people think a clean scan means no vulnerabilities. A scanner finds only packages it can identify and flaws already in its database. A program copied in by hand, or a flaw not yet published, stays invisible.

Check

Have a go

Check

3 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Lock files show a project's libraries

Your own code pulls in libraries too, and its lock file lists them with exact versions. Trivy reads requirements.txt for Python and gradle.lockfile for Java with trivy fs, a scan of a folder.

The shop is a Flask app pinned in 2022. Its requirements.txt gives 49 findings: 49 CVEs in 7 packages, each with a fix.

Scan the shop's requirements.txt

Command
docker exec scanner trivy fs /targets/shop --format json -o /work/reports/shop.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count the shop

Command
docker exec tools python3 steps/count.py shop

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Your turn: scan the Java service

Command
docker exec scanner trivy fs /targets/reports --format json -o /work/reports/reports.json

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Count the Java service

Command
docker exec tools python3 steps/count.py reports

Shown here, not run. Press Start lab to type it into your own lab’s terminal.

Assignment: count a scan

Count a scan's findings. Each finding is a dictionary with cve, pkg, severity and fixed, an empty string when there is no fix yet.

by_severity(findings) returns a dictionary from each severity to how many findings have it. unique_cves(findings) returns each CVE once, in the order first seen. fixable(findings) returns how many findings have a fixed version.

Code: python
def by_severity(findings):
    return {}


def unique_cves(findings):
    return []


def fixable(findings):
    return 0

Make a free account to save your work and track your progress.

Exit quiz

Have a go

Exit quiz

5 questions
Have a go: your answers are marked here and then forgotten. Sign in if you would like your score kept, and to pick up where you left off.Sign in to keep your score

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.