TExcelTAD
Home › CI/CD Pipelines & Pipeline Security › Container Scanning as a CI Gate

Container Scanning as a CI Gate

A container image carries a whole user space: an OS package set, language runtimes and your dependencies. Most of its known vulnerabilities come from the parts you didn't write. Scanners such as Trivy and Grype read the image's package databases and lockfiles and match them against vulnerability data. Software Supply Chain Security explains how the data works (CVE, GHSA, EPSS & KEV), how both scanners report, why they disagree and how to write gates. This lesson is the DevOps view: where the scans go in a pipeline, what they gate, and how the gate stays usable.

The scanners here use vulnerability databases frozen in October 2026, so counts are those of that day; in your pipeline they change daily, which is exactly why scheduled re-scans exist.

Three scans, three places in the pipeline

Scan Command When
Dependencies (SCA) trivy fs --scanners vuln ., grype dir:. every PR: fails fast, before any image exists
Dockerfile / IaC misconfiguration trivy config . PRs touching Dockerfiles, Kubernetes manifests, Terraform
Image trivy image <ref>, grype <ref> after build, before push (the gate); then nightly against what is deployed

The first two run on the repository, so they belong next to the unit tests:

bash · try it yourself
mkdir app && cd app
cat > Dockerfile <<'DF'
FROM python:3.13
COPY . /app
RUN pip install -r /app/requirements.txt
CMD ["python", "/app/main.py"]
DF
cp /opt/samples/python-app/requirements.txt .
cat requirements.txt
echo "### dependencies (HIGH and CRITICAL with a fix)"
trivy fs -q . --scanners vuln --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 -f json -o deps.json
echo "exit $?"
jq -r '[.Results[]?.Vulnerabilities[]? | "\(.Severity) \(.PkgName) \(.InstalledVersion)"] | group_by(.) | map("\(.[0]) (\(length) CVEs)") | .[]' deps.json
echo "### Dockerfile"
trivy config -q . --severity HIGH,CRITICAL --exit-code 1 -f json -o config.json
echo "exit $?"
jq -r '.Results[]?.Misconfigurations[]? | "\(.Severity) \(.ID): \(.Title)"' config.json
requests==2.25.0
urllib3==1.26.4
jinja2==2.11.2
### dependencies (HIGH and CRITICAL with a fix)
exit 1
HIGH urllib3 1.26.4 (8 CVEs)
### Dockerfile
exit 1
HIGH DS-0002: Image user should not be 'root'

Both gates failed, for different reasons: an old pinned urllib3 with eight fixable HIGH vulnerabilities (the other two pins had nothing at HIGH or above with a fix), and a Dockerfile without a USER instruction, so the container would run as root (Docker security). The fixes are a dependency bump and one Dockerfile line, found minutes after the change was pushed.

The image gate

The image scan runs on the built candidate before it is pushed, as in the build → scan → push → deploy workflow. Trivy decides with --severity and --exit-code, Grype with --fail-on; both can drop findings that have no fix yet:

bash · try it yourself
img=/opt/samples/images/alpine-3.18.0.tar
crane push $img localhost:5000/shop/base:candidate > /dev/null 2>&1
trivy image -q --input $img --severity CRITICAL --exit-code 1 -f json -o trivy.json
echo "trivy CRITICAL gate: exit $?  ($(jq '[.Results[].Vulnerabilities[]?] | length' trivy.json) findings)"
grype localhost:5000/shop/base:candidate -q --fail-on critical -o json > grype.json
echo "grype --fail-on critical: exit $?  ($(jq '[.matches[] | select(.vulnerability.severity == "Critical")] | length' grype.json) critical)"
jq -r '[.Results[].Vulnerabilities[]? | "\(.VulnerabilityID) \(.PkgName) \(.InstalledVersion) -> \(.FixedVersion)"] | .[]' trivy.json
trivy CRITICAL gate: exit 1  (3 findings)
grype --fail-on critical: exit 2  (5 critical)
CVE-2022-48174 busybox 1.36.0-r9 -> 1.36.1-r1
CVE-2022-48174 busybox-binsh 1.36.0-r9 -> 1.36.1-r1
CVE-2022-48174 ssl_client 1.36.0-r9 -> 1.36.1-r1

The two scanners agree that this base image must not ship (exit 1 for Trivy, 2 for Grype), though not on the count: Trivy reports one CVE in three busybox packages, Grype five critical matches, because their data sources and matching differ. Grype read the image from the registry, Trivy from the tarball: in a pipeline, scan whichever the build produced, and record the digest you scanned.

Accepting a risk without disabling the gate

Sometimes a finding can't be fixed today: no patched base image yet, or the vulnerable code path is unreachable. Don't lower the threshold for everything; record the exception, with a reason and an expiry date, in a file under review:

bash · try it yourself
cat > .trivyignore.yaml <<'YML'
vulnerabilities:
  - id: CVE-2022-48174
    statement: busybox ash is not reachable from the service; base image upgrade tracked in SHOP-142
    expired_at: 2027-01-31
YML
img=/opt/samples/images/alpine-3.18.0.tar
trivy image -q --input $img --severity CRITICAL --exit-code 1 --ignorefile .trivyignore.yaml -f json -o t.json
echo "with the exception: exit $?"
sed -i 's/2027-01-31/2026-01-01/' .trivyignore.yaml
trivy image -q --input $img --severity CRITICAL --exit-code 1 --ignorefile .trivyignore.yaml -f json -o t.json
echo "after the exception expired: exit $?"
with the exception: exit 0
after the exception expired: exit 1

When the date passes, the finding comes back and the gate fails again, which forces someone to look at it. Grype has the same idea in .grype.yaml ignore: rules; for a standard, shareable format use OpenVEX statements (VEX, Ignore Rules That Age Well).

Keep the database fresh, but pinned in tests

In real pipelines the scanners download their databases on every run (Trivy from ghcr.io / mirror.gcr.io, Grype from Anchore's CDN). Two practical points: cache the database between runs (Trivy's --cache-dir, the actions' built-in caching) so a rate limit doesn't turn every build red, and don't freeze it in production pipelines: a frozen database stops finding new CVEs. This course freezes it only so the outputs stay reproducible.

Test yourself

Which scan can fail a pull request before any image is built?

  1. trivy image
  2. trivy fs / grype dir on the lockfiles, and trivy config on the Dockerfile
  3. A nightly rescan
  4. None

Why give every accepted-risk exception an expiry date?

  1. Trivy requires it
  2. So the finding returns and the gate fails again when the date passes, forcing a re-review
  3. To save disk space
  4. Dates sort the report

An image passed the gate last month and hasn't changed. Can it have critical findings today?

  1. No
  2. Yes: new vulnerabilities are published for packages it already contains
  3. Only if rebuilt
  4. Only on Alpine