Skip to content

Docker image security scanning with Trivy and Grype

Scan container images for CVEs at build time, on push, and on a schedule. How to wire Trivy and Grype into CI, set thresholds, and act on findings.

Every image you ship inherits the CVEs of its base, the runtime libraries it pulls in, and the application dependencies layered on top. A base image that was clean when you built it may have a critical CVE published against it two weeks later. Without scheduled scanning, you’ll only learn about it when someone reports an incident. Trivy and Grype are the two open-source scanners that turn this into a tractable operational discipline.

How to verify

Check whether an image is being scanned at all and what its current vulnerability profile looks like:

trivy image --severity HIGH,CRITICAL ghcr.io/example/web:1.42.0
grype ghcr.io/example/web:1.42.0 --fail-on high

# What's in the image — the SBOM
syft ghcr.io/example/web:1.42.0 -o spdx-json | jq '.packages | length'

# Layer-by-layer scan
trivy image --severity CRITICAL --format table --list-all-pkgs ghcr.io/example/web:1.42.0

If neither tool is installed: brew install trivy grype syft on macOS, or the GitHub Releases binaries on Linux. If a scan returns thousands of low-severity findings, you’re looking at the universe of all packages — filter aggressively or you’ll never act on the real issues.

What’s happening

A container image is layers of filesystem extracted from a Linux distribution plus your application’s installed packages. Scanners walk those layers, build a software bill of materials (SBOM) listing every installed package and its exact version, and compare each entry against vulnerability databases (NVD, GitHub Advisory Database, OSV, language-specific ecosystems). The output is a list of CVEs with severity, affected package, and (sometimes) a fixed version.

Two complementary tools: Trivy (Aqua Security) is the de facto standard, fast, has a comprehensive vulnerability DB, supports OS packages and language ecosystems, and ships an SBOM mode. Grype (Anchore) has slightly different DB coverage, integrates well with Syft (its SBOM tool), and tends to find different things. Running both in CI catches what either misses.

Severity matters: critical CVEs with known exploits in the wild need immediate action; medium and low CVEs are tracked but not deploy-blocking. Set your thresholds explicitly or the noise drowns the signal.

The procedure

  1. Install Trivy in CI. For GitHub Actions:
# .github/workflows/image-scan.yml
name: image-scan
on:
  push:
    branches: [main]
  schedule:
    - cron: '0 4 * * *'  # daily at 04:00 UTC

jobs:
  scan:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4

      - name: Build image
        run: docker build -t myapp:${{ github.sha }} .

      - name: Trivy scan (fail on critical+high)
        uses: aquasecurity/trivy-action@master
        with:
          image-ref: myapp:${{ github.sha }}
          severity: CRITICAL,HIGH
          exit-code: '1'
          ignore-unfixed: true
          format: 'sarif'
          output: 'trivy.sarif'

      - name: Upload SARIF to GitHub Security
        if: always()
        uses: github/codeql-action/upload-sarif@v3
        with:
          sarif_file: 'trivy.sarif'

ignore-unfixed: true filters CVEs that have no fix available — actionable findings only. exit-code: '1' fails the build on any HIGH or CRITICAL finding.

  1. Add a second pass with Grype to catch what Trivy misses:
      - name: Grype scan
        uses: anchore/scan-action@v3
        with:
          image: myapp:${{ github.sha }}
          severity-cutoff: high
          fail-build: true
  1. Scan on a schedule, not only on push. CVEs land daily — an image you built two weeks ago can become vulnerable today:
# Standalone cron entry for nightly scan
0 4 * * * trivy image --severity HIGH,CRITICAL --ignore-unfixed \
  --format json --output /var/log/trivy/$(date +\%Y\%m\%d).json \
  ghcr.io/example/web:latest 2>&1 | logger -t trivy-cron

The nightly scan should pull the same tag you’re running in production. New findings trigger an alert (PagerDuty, Slack, email — whatever your routing is).

  1. Maintain an ignore file for accepted risks. Trivy supports .trivyignore:
# .trivyignore — CVEs we've reviewed and accepted (with expiry)
CVE-2023-12345  # accepted: not exploitable in our config, expires 2026-12-31
CVE-2024-67890  # waiting on upstream fix, expires 2026-09-30

Always include a rationale and an expiry. An ignore without a date never gets revisited.

  1. Generate and store SBOMs alongside images. Then later scans don’t need access to the image:
syft ghcr.io/example/web:1.42.0 -o cyclonedx-json > sboms/web-1.42.0.cdx.json

# Scan the SBOM, not the image:
trivy sbom sboms/web-1.42.0.cdx.json
grype sbom:sboms/web-1.42.0.cdx.json

SBOMs make supply-chain audits answerable. “Are we using log4j?” — grep log4j sboms/*.json.

  1. For local development, scan before pushing:
docker build -t myapp:dev .
trivy image --severity CRITICAL,HIGH --exit-code 1 myapp:dev

Wire this into a pre-push git hook if your team has the discipline; otherwise rely on CI.

  1. When a finding lands, the response is:
    • Severity CRITICAL with known exploit → rebuild with patched base, deploy this hour.
    • Severity HIGH → next regular release window, max one week.
    • Severity MEDIUM/LOW → tracked, batched into a quarterly base-image refresh.

Common pitfalls

  • Failing the build on every severity, including UNKNOWN and LOW — engineers will mass-add to .trivyignore and the signal becomes worthless. Start with CRITICAL+HIGH only.
  • Scanning only at build time and never re-scanning deployed images. The image you built last month has new CVEs against it now.
  • Ignoring CVEs without expiry — that ignore lives forever, hides a re-discovery later, and accumulates technical debt.
  • Running scanners with stale DBs. Trivy updates its DB on every run by default; pin a DB cache only if you understand you’re trading freshness for speed.
  • Treating a scan pass as security assurance. Scanners find known CVEs in known packages; they don’t find configuration weaknesses, hardcoded secrets, or supply-chain compromises before disclosure.

We bake Trivy + Grype + daily scheduled rescans into every client image pipeline, alert on net-new HIGH/CRITICAL findings, and use the SBOMs to answer “are we affected” questions during disclosures. It’s part of managed operations — the alerting goes to our duty roster, not just to a dashboard.