Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

Scan Every Container Before It Ships: Trivy in CI

A couple of years ago I got a CVE alert email about a container running in production. Not a new CVE — one that had been public for eight months. The image was fine when we built it; it had just been sitting there while

A couple of years ago I got a CVE alert email about a container running in production. Not a new CVE — one that had been public for eight months. The image was fine when we built it; it had just been sitting there while the world moved on. That was the day I stopped treating vulnerability scanning as a nice-to-have and made it a build gate.

The tool I landed on was Trivy — free, open source, absurdly fast, and it doesn't just scan container images. If you have a CI pipeline (and after the GitHub Actions article, you do), this is a thirty-minute upgrade that pays for itself the first time it catches something.

One minute to your first scan

Install is a single binary (or a Docker image if you prefer):

curl -sfL https://get.aquasec.io/trivy | sh

Then point it at any image:

trivy image nginx:1.27-alpine

What you get back is a full bill of materials: OS packages, language dependencies (the ones baked into your app images), each with its CVE ID, severity, and — the part I love — whether a fixed version exists. "There's a critical CVE in libssl and upgrading to the current base image fixes it" is an actionable sentence. A raw CVE count is not.

Two flags you'll use constantly:

trivy image --severity HIGH,CRITICAL --exit-code 1 myapp:latest

--exit-code 1 is the whole trick: Trivy stops being a report and becomes a gate. Pipe that into CI and the build fails when a serious CVE ships.

The CI gate

In GitHub Actions, after your build step:

- name: Scan image
  uses: aquasecurity/trivy-action@master
  with:
    image-ref: 'ghcr.io/clinerds/app:${{ github.sha }}'
    severity: 'HIGH,CRITICAL'
    exit-code: '1'

In GitLab CI, a plain script step does the same job:

scan:
  stage: test
  image:
    name: aquasec/trivy:latest
    entrypoint: [""]
  script:
    - trivy image --severity HIGH,CRITICAL --exit-code 1 $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA

A few hard-won settings: scan the image you actually ship (by digest or immutable tag, never :latest — you'll rescan whatever is newest, not what you built). Run the scan on every merge request and nightly — new CVEs appear under old images. And cache the vulnerability DB (--cache-dir or the action's built-in caching); the default download on every run gets old fast.

My own CI templates carry a Trivy job wired exactly like this — because copy-paste security is better than no security.

Managing the noise: the ignore file, with discipline

Within a week, one of three things happens: you fix every finding (congratulations), you disable the gate (don't), or you learn to triage. Triage means .trivyignore — but treat it like a loaded weapon:

# CVE-2025-12345 — false positive for our usage, we don't call that code path
# reviewed 2026-11-14 by faizan, revisit on next base image bump
CVE-2025-12345

Two rules I enforce on myself: every ignored CVE gets a comment explaining why and when to revisit, and the ignore file gets audited monthly. An uncommented CVE in that file is how eight-month-old vulnerabilities end up in production.

Also set realistic expectations with --severity. Start with HIGH,CRITICAL as the gate and let MEDIUM be visible-but-nonblocking; going straight to "zero findings including LOW" is how teams end up disabling the whole thing in frustration.

The part most people miss: it's not just images

Trivy scans three other things, and two of them matter more than images for most teams:

Misconfigured infrastructure:

trivy config .

Point it at a directory of Terraform, Kubernetes manifests, or Dockerfiles and it flags misconfigurations — containers running as root, missing resource limits, wide-open security contexts, S3 buckets that rhyme with "public." This is the cheapest compliance win I know of: no agents, no cloud permissions, just YAML judgment.

Secrets in the repo:

trivy fs --scanners secret .

It catches committed AWS keys, private keys, and the usual suspects. Pair it with a pre-commit hook so the secret never lands in history at all — rotating a key that was committed for four minutes is a worse afternoon than it sounds.

Filesystem scans on hosts (trivy fs) work for scanning a server after the fact, which I've used more than once during first-30-minutes hardening on a box I didn't build.

Generate an SBOM while you're at it

One command turns your image into a Software Bill of Materials:

trivy image --format cyclonedx --output sbom.json myapp:1.4.2

If a customer, auditor, or future-you ever asks "what's actually in this image," the answer is a file, not an archaeology project. CycloneDX format slots into dependency-track and similar tools if you ever go further.

The honest catch

Scanners find known vulnerabilities in packages. They will not find your application-level bugs, your overly permissive IAM policy, or the SSH key your intern committed in a different repo. Trivy in CI is a seatbelt, not a self-driving car — but it's a seatbelt that takes thirty minutes to install and has saved me from shipping a critical OpenSSL CVE more than once.

Ship the gate first. Worry about the rest of the DevSecOps checklist after the pipeline stops you from doing something dumb automatically — that's the whole point of automation.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.