Skip to main content

Containers and Kubernetes · Intermediate · 16 min

Find vulnerable packages in images and SBOMs with Grype

By Padding · Updated 18 Nov 2026
Ubuntu 26.04 · Grype 0.122.1 · Syft 1.57.2

You will install Grype 0.122 with a checked download, scan a public image straight from its registry, save an SBOM and re-scan it in seconds, gate a build with --fail-on, and record ignore rules with written reasons.

On this page

Grype is Anchore's open-source vulnerability scanner. You point it at a container image, a directory or a software bill of materials (SBOM), it works out which packages are present, and it matches them against a vulnerability database it downloads and keeps on your machine. It does not need Docker: it can pull an image straight from a registry.

This tutorial is built around one habit that makes scanning cheap: catalogue an image once, save that catalogue as an SBOM file, and scan the file. The package list of a released image never changes, but the list of known vulnerabilities grows every day. Re-scanning a saved SBOM against a fresh database tells you about new CVEs in seconds, without pulling the image again.

You will practise on alpine:3.18.0, a small, old Alpine release that is past end of life and has many published vulnerabilities, which is exactly what you want for learning. Do not use it for anything else. Everything happens in a throwaway folder, ~/grype-demo.

Step 1 · Install Grype from the release with a checksum check

Install the tools the steps use, then download Grype 0.122.1 for your CPU architecture with its checksum file. dpkg --print-architecture prints amd64 or arm64, which matches the names of the release files:

Bash
sudo apt update
sudo apt install -y curl ca-certificates jq
mkdir -p ~/grype-demo && cd ~/grype-demo
GRYPE_VERSION=0.122.1
ARCH=$(dpkg --print-architecture)
curl -sSLO "https://github.com/anchore/grype/releases/download/v${GRYPE_VERSION}/grype_${GRYPE_VERSION}_checksums.txt"
curl -sSLO "https://github.com/anchore/grype/releases/download/v${GRYPE_VERSION}/grype_${GRYPE_VERSION}_linux_${ARCH}.tar.gz"
grep " grype_${GRYPE_VERSION}_linux_${ARCH}.tar.gz\$" "grype_${GRYPE_VERSION}_checksums.txt" | sha256sum -c -
tar -xzf "grype_${GRYPE_VERSION}_linux_${ARCH}.tar.gz" grype
sudo install -m 755 grype /usr/local/bin/grype
rm grype "grype_${GRYPE_VERSION}_linux_${ARCH}.tar.gz"

sha256sum -c must print OK for the archive. If it prints FAILED, delete the download and stop: the file is not the one Anchore published. The project also offers a one-line install script; downloading the archive yourself means you see and check exactly what you run.

A binary installed this way gets no apt security updates. To upgrade, check the releases page, set GRYPE_VERSION to the new version and run the same commands again. By default Grype also checks for a newer release when it runs and prints a notice if one exists (the check-for-app-update setting turns this off).

Check it worked

Bash
grype version

The first line reads Application: grype and the Version: line shows 0.122.1.

Step 2 · Download the vulnerability database

Grype matches packages against a local database built from sources such as the distribution security trackers, the GitHub Advisory Database and the NVD. Fetch it now, so the first scan is not slowed down by the download:

Bash
grype db update
grype db status

The database lives in ~/.cache/grype/db. By default Grype checks for a newer database every time it runs, and refuses to scan with one that is more than five days old, because an old database quietly misses new CVEs.

If you pay for data by the megabyte, as many people on prepaid mobile plans across Africa do, the database is a far bigger download than a small image. Run grype db update once on Wi-Fi, then turn off the check on each run with GRYPE_DB_AUTO_UPDATE=false until you are on a cheaper connection again. Within the five-day limit, scans then use no data at all for SBOM files.

Check it worked

grype db status prints the database location, when it was built and its schema version, and a status line that says something like valid. A built date older than five days means the update did not happen; run grype db update again.

Step 3 · Scan an image straight from the registry

The registry: prefix tells Grype to pull the image layers from the registry itself, with no Docker daemon involved. Write the report to a file as well as reading it on screen:

Bash
cd ~/grype-demo
grype registry:alpine:3.18.0 --file image-report.txt
head -n 15 image-report.txt

Each row of the table is one match: a package, the installed version, the version that fixes it (if there is one), the package type (apk here), the vulnerability ID and its severity. Grype 0.122 also shows an EPSS column (the estimated chance the flaw is exploited in the next 30 days) and a risk score that combines it with severity, and sorts rows by that risk score unless you pass --sort-by. Expect many rows for libcrypto3 and libssl3, which come from OpenSSL, and for busybox.

For a production image, pin the image by digest (alpine@sha256:...) rather than by tag, so you know exactly what you scanned.

Check it worked

Bash
cd ~/grype-demo
grep -c 'apk' image-report.txt

The count is well above zero. Every finding in this image is an Alpine package, so each row contains apk.

Step 4 · Save an SBOM with Syft and scan the file

Grype uses Syft, Anchore's SBOM tool, internally to catalogue packages. Install the Syft binary the same checked way and save the catalogue of the image as Syft JSON. The Syft tutorial covers SBOM formats in depth; here you only need the file:

Bash
cd ~/grype-demo
SYFT_VERSION=1.57.2
ARCH=$(dpkg --print-architecture)
curl -sSLO "https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_checksums.txt"
curl -sSLO "https://github.com/anchore/syft/releases/download/v${SYFT_VERSION}/syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz"
grep " syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz\$" "syft_${SYFT_VERSION}_checksums.txt" | sha256sum -c -
tar -xzf "syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz" syft
sudo install -m 755 syft /usr/local/bin/syft
rm syft "syft_${SYFT_VERSION}_linux_${ARCH}.tar.gz"
syft registry:alpine:3.18.0 -o syft-json=alpine-3.18.0.syft.json

Now scan the SBOM instead of the image. The sbom: prefix reads the file from disk; time shows how long it takes:

Bash
cd ~/grype-demo
time grype sbom:./alpine-3.18.0.syft.json --file sbom-report.txt

No layers are downloaded, so this takes a few seconds even on a slow connection. In practice you generate the SBOM when you build and publish an image, keep it next to the image (as a release file or an attestation), and re-scan the SBOM every night. A new CVE in a package you shipped months ago then shows up the next morning, with no rebuild or pull. You can also pipe an SBOM in: cat alpine-3.18.0.syft.json | grype.

Check it worked

Bash
cd ~/grype-demo
wc -l image-report.txt sbom-report.txt

The two reports have the same number of lines, or differ by a row or two at most: the SBOM holds the same package list Grype found in the image. A small difference can come from the copy of Syft built into Grype being a slightly different version from the Syft binary you installed.

Step 5 · Read the results as JSON with jq

The table is for people. For scripts and dashboards, write JSON and query it with jq:

Bash
cd ~/grype-demo
grype sbom:./alpine-3.18.0.syft.json -o json --file report.json
jq '.matches | length' report.json
jq -r '.matches[] | .vulnerability.severity' report.json | sort | uniq -c | sort -rn
jq -r '.matches[] | select(.vulnerability.severity == "Critical" or .vulnerability.severity == "High")
  | [.artifact.name, .artifact.version, .vulnerability.id, (.vulnerability.fix.versions | join(","))] | @tsv' report.json | head

The first command prints the number of matches, the second a count per severity, and the third a tab-separated list of high and critical findings with the version that fixes each one. Each match also has .vulnerability.fix.state, which is fixed, not-fixed, wont-fix or unknown.

Check it worked

Bash
cd ~/grype-demo
jq -e '.matches | length > 0' report.json
jq -r '[.matches[] | .artifact.name] | unique | join(" ")' report.json

jq -e prints true, and the second command lists the affected package names, including libcrypto3.

Step 6 · Gate a build with --fail-on and --only-fixed

In a pipeline you want the scan to fail the job when it finds something serious. --fail-on high makes Grype exit with a non-zero code when any match is high or critical. Add --only-fixed to leave out findings that have no fixed version yet, so the gate only blocks on problems a team can act on by upgrading:

Bash
cd ~/grype-demo
grype sbom:./alpine-3.18.0.syft.json -o json --only-fixed --file fixed.json
echo "all: $(jq '.matches | length' report.json)  fixable: $(jq '.matches | length' fixed.json)"
grype sbom:./alpine-3.18.0.syft.json --only-fixed --fail-on high > /dev/null && echo "gate passed" || echo "gate failed, exit code $?"

--only-notfixed does the opposite and is handy for a report of what you cannot fix yet. In CI, run the gate on the SBOM of the image you just built, and keep the full report as a job artefact so people can see why it failed.

Check it worked

The first line shows the fixable count is no higher than the total. The last line prints gate failed, exit code followed by a non-zero number. Change high to a level no finding reaches and it would print gate passed. On an old Alpine release almost everything is fixable, because newer packages exist in the same branch.

Step 7 · Export SARIF for code scanning tools

SARIF is the standard JSON format that code-scanning dashboards, including GitHub code scanning, accept for security findings. Grype writes it directly:

Bash
cd ~/grype-demo
grype sbom:./alpine-3.18.0.syft.json -o sarif --file grype.sarif
jq -r '.version, .runs[0].tool.driver.name, (.runs[0].results | length)' grype.sarif

The first GitHub Actions tutorial shows how a workflow runs commands; uploading grype.sarif from such a workflow puts each finding next to your code.

Check it worked

The command prints the SARIF version 2.1.0, the tool name Grype (or similar casing) and a result count above zero.

Step 8 · Ignore a finding with a written reason

Sometimes a finding does not apply: the vulnerable code path is not used, or a fix is planned and accepted for a short time. Record that in .grype.yaml in the project folder, which Grype reads automatically. A rule ignores a match only when every field in it matches, and the reason field is a note stored with the ignored match. Grype has no expiry date for rules, so put a review date in the reason and check it when you next touch the file.

This rule ignores one OpenSSL CVE from the report. Replace the ID and reason with your own when you use this on a real image:

Bash
cd ~/grype-demo
cat > .grype.yaml <<'YAML'
ignore:
  - vulnerability: CVE-2023-5363
    reason: "Demo only: this image never uses OpenSSL with custom key or IV lengths. Owner: platform team. Review by 2027-02-01."
YAML
jq -r '.matches[] | .vulnerability.id' report.json | grep -c '^CVE-2023-5363$'
grype sbom:./alpine-3.18.0.syft.json -o json --file report-ignored.json
jq -r '.matches[] | .vulnerability.id' report-ignored.json | grep -c '^CVE-2023-5363$' || echo "CVE-2023-5363 no longer reported"

Ignore by CVE and, where you can, also by package name, so the rule cannot hide the same CVE in a different package later. Avoid broad rules such as a whole package or fix-state: not-fixed for everything: they turn the scan into a list nobody reads.

Check it worked

The first grep -c prints a number above zero (usually 2, one match each for libcrypto3 and libssl3), and the last command prints 0 (grep found no matches) followed by CVE-2023-5363 no longer reported. The ignored matches are not lost: they move to the ignoredMatches list in report-ignored.json, each with the rule that hid it. To see suppressed findings in the table, with the rest, run grype sbom:./alpine-3.18.0.syft.json --show-suppressed; the ignored rows are marked (suppressed).

Step 9 · See why Grype and Trivy can disagree

If you also scan the same image with Trivy, the counts will rarely match. That is not a bug in either tool. They build their databases from overlapping but different sources, refresh them at different times, and match packages differently: one may use only the distribution's own advisories for OS packages while the other also uses CPE matching against the NVD, and they can disagree on severity when the distribution and the NVD rate a CVE differently. Grype records how each match was made:

Bash
cd ~/grype-demo
jq -r '.matches[] | [.vulnerability.namespace, .matchDetails[0].type, .matchDetails[0].matcher] | @tsv' report.json | sort | uniq -c

Check it worked

You see a few lines with a count each, something like alpine:distro:alpine:3.18 exact-indirect-match apk-matcher. An indirect match means Grype matched the source package (openssl) that the installed package (libcrypto3) was built from. When two scanners disagree about a finding, compare this namespace and match type with the other tool's data source before you decide which one to trust, and check the distribution's own tracker for the final word on OS packages.

When you are finished, remove the demo folder. Keep Grype and Syft if you will use them again, or delete them too:

Bash
rm -rf ~/grype-demo
sudo rm -f /usr/local/bin/grype /usr/local/bin/syft
rm -rf ~/.cache/grype

Next steps

Need this done for you?

Padding does this work for growing companies, from the first diagnosis to a system that keeps working.

Platform Engineering

Get new tutorials

New tutorials, about twice a month.