IT-Admin.tech

Container image scanning in CI/CD: Trivy, Clair, OPA & Policy‑Gates

CI/CD-Architekturdiagramm mit Trivy-, Clair- und OPA-Integrationen zur Image-Prüfung
Schematische Pipeline: Build → Trivy/Clair Scan → OPA Policy-Gate → Registry. Architektur zeigt Integrationspunkte, SBOM-Flow und Signaturprüfung für Audit und Governance.

Container image scanning in CI/CD is no longer a nice-to-have but an operational obligation for teams that bring containers into production. Remember the focus keyword: Container-Image-Scanning in CI/CD – scan early, filter critically, deploy safely. In this article I explain in a practical manner the role tools like Trivy and Clair play, how policy-as-code with Open Policy Agent (OPA) automates gate decisions, and which operational and migration pitfalls you should avoid.

Why image scanning in the pipeline is indispensable

Container images bundle operating system packages, runtime libraries and application artifacts. A security issue in any layer — an insecure library, an unprotected service, incorrect file permissions — can become a production risk. Image scanning looks for known vulnerabilities (Common Vulnerabilities and Exposures, CVE), outdated components or configuration errors.

Terms briefly explained: CVE denotes a publicly known vulnerability. CVSS (Common Vulnerability Scoring System) is a metric for prioritizing CVEs. SBOM (Software Bill of Materials) is a list of all components of an image that provides transparency for audits and scans. Registry is the location (e.g. Docker Hub, Harbor) where images are stored. CI/CD refers to Continuous Integration/Continuous Delivery, i.e. automated build and delivery processes.

Which scan targets and timings are there?

Scanning can occur at multiple points; each position has advantages and disadvantages:

  • Build time: Scans during the build (in the CI) detect issues early. Advantage: short feedback loops; disadvantage: performance impact and false positives that can block builds.
  • Registry scanning: Scans after pushing to the registry (asynchronous) are suitable when registry integrations exist. Advantage: consistent scans of all images; disadvantage: deployments might use an image before the scan has completed.
  • Pre-deploy / Policy gate: Gate before production – only images that meet policies are released. Advantage: final security check; disadvantage: increases complexity and requires good rollback strategies.
  • Runtime scans: Scans of running containers via CPE-/SBOM-matching and RASP tools. Advantage: detects drift; disadvantage: is reactive and often resource-intensive.

Tool overview: Trivy and Clair compared

Trivy and Clair are both established scanners but follow different architectures and operational models.

Trivy

Trivy is a lightweight, fast-starting scanner that finds vulnerabilities in operating system packages, application libraries and container images. Trivy uses public CVE data sources and can produce SBOMs. It runs locally in the CI job or as a server (Trivy Server) and has a broad format and integration ecosystem.

Important for operations teams: Trivy is performant, but standard feeds must be updated regularly. Caching can speed up jobs but leads to potential data lag when new CVEs appear.

Shell
# Beispiel: Schneller Scan eines lokalen Images mit Trivy
trivy image --exit-code 1 --severity HIGH,CRITICAL --format json -o trivy-result.json my-registry.example.com/myapp:1.2.3

The –exit-code 1 option ensures that Trivy returns a non-zero exit code when a HIGH/CRITICAL vulnerability is found, which CI systems can interpret as a build failure.

Clair

Clair is an engine for analyzing container images with a focus on persistence and integration into registry workflows. Clair runs a database where scans and vulnerability feeds are consolidated. This makes Clair robust for organizational requirements that need long-term queries, historical analyses, and registry integration.

Operational implication: Clair requires more infrastructure (database, service) and is therefore organizationally more demanding. In large organizations with many images and compliance requirements, however, Clair can provide advantages through better management of scan history.

Shell
# Beispiel: Clair-Scanner (community tool) gegen eine Clair-Instanz
clair-scanner --ip clair-service.internal my-registry.example.com/myapp:1.2.3

Note: There are various scanners/wrappers for Clair (clair, clair-scanner). Check the documentation of the variant you deploy and operate Clair, if possible, in a secure subnet with restricted access.

Policy-as-Code mit OPA: warum und wie

Open Policy Agent (OPA) is a policy engine that describes rules in Rego. Policy-as-Code means: security rules are versioned, reviewed, and applied automatically—comparable to Infrastructure-as-Code. OPA is not a scanner; it makes decisions based on metadata (e.g., scan results, SBOM, image tags) and returns a yes/no decision to a build job.

Examples of policies: disallow base images with known critical CVEs, disallow images with a root-user tag, allow only signed images, require SBOMs, or impose a maximum CVSS threshold.

Rego
package image.policy

# Verweigere Images mit kritischen Schwachstellen
violation[reason] {
  some vuln
  input.scanResults.vulnerabilities[vuln]
  vuln.severity == "CRITICAL"
  reason = sprintf("CRITICAL vulnerability: %s", [vuln.vulnerabilityID])
}

default allow = true
allow { count(violation) == 0 }

Integrate OPA into CI as a gate: the CI reads scan JSON (e.g., Trivy output), calls opa eval or opa test, and aborts the job if policies are violated.

Shell
# Beispiel: Policy-Prüfung mit OPA (lokal im CI-Job)
opa eval -i trivy-result.json -d policy.rego "data.image.policy.allow"
# Exit-Code auswerten und Build abbrechen, wenn policy.allow == false

Architekturvorschlag: Wie die Komponenten zusammenspielen

A common architecture combines fast in-line scans during the build, asynchronous registry scans, and OPA gates before production:

  1. Build job creates the image and pushes it to the internal registry.
  2. Build job runs Trivy (or Clair-Scanner) and stores results as JSON/artifact.
  3. OPA evaluates the results against policies; on violation the build fails or marks the image as „quarantined“.
  4. The registry runs additional scans (e.g., Clair) and persists the history.
  5. The deployment job queries the registry status and the OPA/policy API before triggering a live rollout.

Important: Separation of scan results and policy decision enables auditability and traceability. Store scan JSONs in your CI artifact storage or in a central security backend (e.g., Harbor, which provides an API for scan results).

Container-Image-Scanning in CI/CD: Gate-Strategien und Praxis

Gate strategies define when a scan triggers an action. Typical gradations:

  • Warning: Result is documented, build continues. Good for the observation phase.
  • Quarantine: Image remains in the registry, deployment blocked until review.
  • Block: Build/deploy is prevented (e.g., for CRITICAL CVEs).

A proven approach is cascading policies: initially warnings, then quarantine for recurring issues, and finally hard blocks for critical risks. Use SBOMs to reduce false positives — if a library appears in the image but is not used at runtime, that can influence the policy decision.

CI-Integration: Beispiel GitLab‑CI mit Trivy und OPA

Practical example: a GitLab CI job that builds an image, scans it with Trivy and uses OPA for the policy decision. The YAML is idempotent and pins versions.

Yaml
stages:
  - build
  - scan

variables:
  IMAGE: registry.example.com/$CI_PROJECT_PATH:$CI_COMMIT_SHORT_SHA

build:
  stage: build
  image: docker:20.10
  services:
    - docker:dind
  script:
    - docker build -t $IMAGE .
    - docker push $IMAGE
  artifacts:
    paths: ["build.log"]

scan:
  stage: scan
  image: aquasec/trivy:0.40.0
  script:
    - trivy image --format json -o trivy-result.json $IMAGE
    - opa eval -i trivy-result.json -d policy.rego "data.image.policy.allow" || exit 1
  artifacts:
    paths: ["trivy-result.json"]
  when: on_success

Important: the OPA evaluation makes the decision; let the scanner itself only provide results. This keeps the semantic decision centrally versioned and auditable.

Migration path: From Trivy-only to Clair + OPA

As your organization grows, you may want to migrate from local CI scans to a hybrid architecture with Clair and OPA. A practical migration path:

  1. Analysis: inventory images, scans per week and average scan time.
  2. Pilot: run Clair in an isolated Namespace with a small DB and configure feed-sync.
  3. Parallel operation: keep Trivy running in CI, but send copies of scan results to Clair for persistence.
  4. Policy hardening: version OPA policies and run tests in CI (opa test).
  5. Rollout: switch deploy jobs to registry status queries and OPA API checks; monitor metrics and SLA failures.

Typical pitfalls during migration: missing capacity for the Clair DB, inconsistent feed versions, and insufficient Slope for exception processes. Plan capacity tests and have a rollback option.

Operational checklist before Go‑Live

  • Pin scanner and OPA versions in CI.
  • Start with observability: warnings instead of blocks for 4–8 weeks.
  • Introduce a ticketing workflow for exceptions with timeboxed approvals.
  • Automate SBOM generation and store artifacts.
  • Sign images with cosign and verify signatures via Admission Controller.
  • Define SLAs for feed-sync, policy review and exception decisions.

Advanced failure cases, troubleshooting and typical causes

Some errors recur. Here are typical causes and diagnostic sequences:

  • Scanner incompatibility: Pin versions; test against a test registry. Check release notes for breaking changes.
  • Network/Proxy/TLS: CI runners often require explicit proxy and CA config. Test connectivity from the runner with curl and trivy db update.
  • Performance bottlenecks: Scans are I/O- and CPU-intensive. Scale runners or operate dedicated scanner containers/servers.
  • Missing audit trails: Store scan JSON, OPA decisions and exception logs centrally for forensic purposes.
Shell
# Netzwerk- und Feed-Check
trivy db update --download-db-only || echo "Feed update failed"
curl -I https://registry.example.com/v2/ || echo "Registry unreachable"

Retention, archiving and forensic requirements

For compliance and incident response, store scan JSONs, OPA decisions and exception reviews for at least 6–24 months. Structure logs so the following fields are easily queryable: Image-Tag, Registry-URL, Scan-Timestamp, Vulnerability-IDs, CVSS, Policy-Decision, Reviewer and approval date. These metadata are critical when you later analyse root causes or respond to regulatory inquiries.

Rollback and emergency strategy

In situations where a gate incorrectly blocks or a critical release is imminent, you need a controlled bypass process:

  • Temporary exception documented in the ticketing system, with rationale, reviewer and expiry date.
  • Canary deploy with feature flags to minimise risk.
  • Fallback image tagging: retain verified, signed images as a rollback option.
  • Automated rollback playbook for orchestration (e.g. Kubernetes rollback via controller).

Every exception is an audit case: post-incident review is mandatory to improve policies in the long term.

Metrics and reporting: what you should measure

Key metrics to manage quality and efficiency:

  • Average scan duration per image
  • Number of policy violations per release
  • False positive rate after triage
  • Feed sync failures per day
  • Mean time to exception approval (SLA)

Automated reports help expose technical debt (e.g. outdated base images) and clarify responsibilities.

Conclusion: pragmatism over perfection

Container image scanning in CI/CD is a balancing act between security and availability. Trivy is well suited for fast, integrated scans; Clair is appropriate when you need registry-centred persistence and history. OPA provides the necessary governance and audit layer to enforce policies automatically. A phased rollout with observability, tested policies, clear SLAs and documented exception processes provides security without compromising delivery.

Further topics and internal linking

This topic pairs well with guides on registry hardening, CI runner security, SBOM generation, image signing and incident response playbooks. Prepare internal links to these areas to build a consistent operational model.

Operational aspects for container image scanning in CI/CD

If you intend to operate scanning rather than just experiment with it, risks shift from tool choice to operational discipline: feed integrity, performance, security hardening of the scanner services and policy governance. Plan technical measures early that ensure the availability and confidentiality of the scan pipeline.

  • Feed management: Mirror vulnerability feeds locally or via CDN to cushion rate limits and external outages. Validate feed integrity (hashes, signatures) and alert on failed synchronizations.
  • Scaling & Caching: Scale scanner workers horizontally; use a shared cache for DB/feeds so parallel CI jobs do not all download the same feeds. Under high load, reduce scan fidelity (e.g., only HIGH/CRITICAL) as a temporary circuit breaker.
  • Secure Communication: Protect Clair, Trivy servers and OPA with mTLS, firewall rules and network segmentation. Store DB credentials and signing keys in a secrets backend (Vault) and rotate them regularly.
  • Policy Governance: Policy changes belong in GitOps workflows with branch protection, peer review and automated opa test runs. Roles and access rights on policy repositories minimize abuse risks.
  • Recurring Tasks: Automate DB maintenance (VACUUM/OPTIMIZE), feed backups and health checks; schedule windows for index rebuilds and capacity tests.
  • Rescan Strategies: Trigger re-scans automatically on new CVEs or on base image updates; orchestrate these jobs so peak loads are controlled.
  • Observability: Log structured scan JSONs with Image‑Digest, Request‑ID and Policy‑Decision. Export metrics for scan‑queue length, decision latency and % of blocked images.
Yaml
# Example: Prometheus alert for long scan queues
- alert: HighScanQueueLength
  expr: sum(container_scanner_queue_length) > 50
  for: 10m
  labels:
    severity: warning
  annotations:
    summary: "Scanner-Queue > 50 (10m)"

Such alerts help trigger automatic degrader rules (e.g., reduced scan depth) while alerting teams. With these operational measures you make container image scanning in CI/CD robust, auditable and scalable — and thereby create a resilient foundation for your digital enterprise solutions.