Guides / Containers

Scan Docker images in CI with Trivy or Grype (GitHub Actions and GitLab CI)

intermediateUpdated 26 Nov 20261041 words

A scanner in the pipeline reads the packages of an image, matches them against vulnerability databases and fails the job above a threshold you choose. Trivy and Grype are open-source scanners for this. Both are single binaries and read OS packages (Debian, Ubuntu, Alpine, RHEL and others) as well as the dependencies of language packages inside the image.

Choose what fails the build

Most findings of a scan do not have a fix. In a test with Trivy 0.75 in November 2026, debian:bookworm-20261027-slim had 253 findings; 3 had a fixed version, 2 of them rated high. A job that fails on all 253 fails on every build. A threshold you can act on:

  • fail on critical and high findings that have a fixed version (Trivy --ignore-unfixed, Grype --only-fixed); a rebuild or a version bump fixes them;
  • print everything else in the log or a report for later review;
  • fail when the base OS is past its end of life (Trivy --exit-on-eol), since fixes stop coming.

Run the scan on every push and on a schedule. A new CVE in a package of an image that has not changed for a month only shows up in a scheduled run.

Trivy on the command line

trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 example/app:1.4.2

--exit-code 1 makes Trivy return 1 when it reports anything; without it Trivy returns 0. For an image saved with docker save, use --input app.tar instead of the name. --exit-on-eol 1 returns 1 when the OS of the image is out of support.

Accepted findings go into .trivyignore in the directory where the scan runs, one ID per line, with a comment that says why. An exp: date ends the exception, and the finding comes back for review:

# libfoo in the image, not reachable from our code
CVE-2026-12345 exp:2027-01-31

Grype on the command line

grype example/app:1.4.2 --fail-on high --only-fixed

--fail-on high makes Grype exit with a non-zero code when it finds a vulnerability of severity high or above. Grype reads images from the Docker daemon, from a registry (registry:example/app:1.4.2) or from a docker save archive (docker-archive:app.tar). Accepted findings go into .grype.yaml:

ignore:
  - vulnerability: CVE-2026-12345
    package:
      name: libfoo

A rule matches only when all of its fields match.

Pin the actions by commit SHA

In March 2026, an attacker with stolen credentials force-pushed 76 of 77 version tags of aquasecurity/trivy-action and all tags of aquasecurity/setup-trivy to commits that stole CI secrets (CVE-2026-33634, advisory GHSA-69fq-xp46-6x23). Workflows that referenced one of these tags, such as @0.28.0, and ran in the roughly 12 hours before the tags were restored, ran the code of the attacker. Workflows pinned to the commit SHA of a release did not. The advisory recommends pinning actions to "full, immutable commit SHA hashes", and the GitHub documentation recommends the same for all actions.

The owner of a repository can move a tag; a full SHA always points to the same commit. Write the version in a comment next to the SHA. Renovate and Dependabot read it and update both.

GitHub Actions with Trivy

name: image
on:
  push:
    branches: [main]
  pull_request:
  schedule:
    - cron: "17 4 * * 1"

permissions:
  contents: read

jobs:
  build-and-scan:
    runs-on: ubuntu-24.04
    steps:
      - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1

      - name: Build
        run: docker build --pull -t app:${{ github.sha }} .

      - name: Scan with Trivy
        uses: aquasecurity/trivy-action@ed142fd0673e97e23eac54620cfb913e5ce36c25 # v0.36.0
        with:
          image-ref: app:${{ github.sha }}
          version: v0.76.2
          severity: CRITICAL,HIGH
          ignore-unfixed: true
          exit-code: "1"

The version input pins the Trivy binary the action installs; without it the action uses its own default. Set it and update it on purpose.

GitHub Actions with Grype

      - name: Scan with Grype
        uses: anchore/scan-action@11983a512817154c80a9db660c1a184be7513ed6 # v7.4.3
        with:
          image: app:${{ github.sha }}
          fail-build: true
          severity-cutoff: high
          only-fixed: true
          output-format: table

fail-build is on by default, with severity-cutoff at medium. The default output format is SARIF; with table the findings appear in the job log.

GitLab CI with Trivy

Run the Trivy image as the job image and scan the image the build job pushed to the project registry:

trivy-scan:
  stage: test
  image:
    name: aquasec/trivy:0.76.2@sha256:8f6bb947dbc528902e11f2f5be52f7b519f6a39d5d148f41166f2040499768fb
    entrypoint: [""]
  variables:
    GIT_STRATEGY: none
    TRIVY_USERNAME: "$CI_REGISTRY_USER"
    TRIVY_PASSWORD: "$CI_REGISTRY_PASSWORD"
    TRIVY_AUTH_URL: "$CI_REGISTRY"
    TRIVY_NO_PROGRESS: "true"
    TRIVY_CACHE_DIR: ".trivycache/"
  script:
    - trivy image --severity HIGH,CRITICAL --ignore-unfixed --exit-code 1 "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
  cache:
    paths:
      - .trivycache/

The Trivy image has trivy as its entrypoint, which would swallow the script lines; entrypoint: [""] clears it. GIT_STRATEGY: none skips the clone; set it back to the default if you keep a .trivyignore in the repository. The image is pinned by digest because in the same incident, malicious Trivy images were pushed to Docker Hub under new tags.

GitLab also ships a template, Jobs/Container-Scanning.gitlab-ci.yml, which runs Trivy and is available on the Free tier. Its findings appear in merge requests and the security dashboard only on GitLab Ultimate.

Cache the vulnerability database

Both tools download a vulnerability database on the first run and refresh it when it is out of date. Cache the directory between runs as in the GitLab example; the Trivy action caches by default. In the test above, the first run of Trivy spent 13 seconds on the download, and later scans of the Debian image took about a second each.

Images that do not pass through CI

CI covers the images you build. Images you pull straight into production, such as databases and proxies, never pass your pipeline, and a CVE published next month applies to the image that runs today. Scan those on a schedule as well, or with a tool that watches the running containers.

The Vulnpocalypse collector sends the package list of every running image, and the daily digest lists new fixable findings per image.

Sources

Check your servers every day

The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.

Start free