Docker Image Scanning: Security Pipeline
Integrate container image scanning into CI/CD pipelines with Trivy, Grype, and Docker Scout — catch vulnerabilities before deployment instead of after the incident report. Here's how the tools compare and how to wire them into real pipelines.
Table of Contents
- Why Scan Images
- Trivy — Fast and Complete
- Grype — SBOM-Focused
- Docker Scout — Built-In
- CI/CD Integration
- Reducing Image Vulnerabilities
- Scanning Strategy
Why Scan Images
Container images layer on each other — your app image inherits every vulnerability in the base image plus whatever your layers add:
nginx:alpinemight have 20 known CVEs- Your
node:20base might have 50 npm installmight pull in vulnerable packages- OS packages (
apt install) add more attack surface
The key insight: most of what a scanner finds isn't your code — it's the base image, the OS packages, and transitive dependencies you never chose directly. That means two things: scanning finds problems you didn't know you had, and the fixes are often mechanical (rebuild on a newer base, bump a dependency) rather than engineering work.
Scanning also decays — an image that was clean at build time isn't clean next month, because new CVEs are published against old packages. Rescanning stored images periodically matters, not just scanning at build.
Trivy — Fast and Complete
Trivy is the default choice for most teams: single binary, no daemon, scans OS packages and language dependencies, and detects IaC misconfigurations and secrets in the same pass.
Basic Scan
# Scan an image
trivy image myapp:latest
# Scan for specific severities only
trivy image myapp:latest --severity HIGH,CRITICAL
# Output as JSON for parsing
trivy image myapp:latest --format json -o trivy-report.json
--severity is the flag that makes scanning usable — an unfiltered report shows every LOW and MEDIUM finding and nobody reads it. Start by gating on HIGH/CRITICAL only.
Scan in CI
# Fail pipeline on HIGH/CRITICAL
trivy image myapp:latest --severity HIGH,CRITICAL --exit-code 1
# Scan and generate HTML report
trivy image myapp:latest --format template --template '@contrib/html.tpl' -o report.html
--exit-code 1 is what turns a scan into a gate: findings fail the pipeline, the image never reaches the registry, and the fix happens in the PR — not in production.
Scan SBOM
# Generate SBOM first
trivy image --format cyclonedx -o sbom.json myapp:latest
# Or scan an existing SBOM
trivy sbom sbom.json
The SBOM is the artifact worth keeping — a complete dependency inventory that compliance asks for and that makes "are we affected by the CVE that just dropped?" a grep instead of an archaeology project.
Grype — SBOM-Focused
Anchore's Grype is Trivy's main rival — same job, different scanner. It pairs with Syft (their SBOM generator) for a clean two-step workflow:
# Scan image
grype myapp:latest
# Fail on high severity
grype myapp:latest --fail-on high
# Scan from SBOM
syft myapp:latest -o syft-json > sbom.json
grype sbom:sbom.json
The Syft→Grype split is useful when you want the SBOM as a first-class artifact (attach it to releases, diff dependency changes between versions) rather than a scan byproduct. In practice, Trivy and Grype find nearly identical results — pick whichever integrates better with your pipeline; running both is rarely worth the noise.
Docker Scout — Built-In
Scout ships with Docker Desktop and the CLI — zero install, good for local iteration before pushing:
# Scout (built into Docker Desktop/CLI)
docker scout cves myapp:latest
# Compare with base image
docker scout compare --to nginx:alpine myapp:latest
# Get recommendations
docker scout recommendations myapp:latest
compare is the useful one — it shows which CVEs came from your base image vs. your own layers, which tells you whether the fix is "rebuild on a newer base" or "patch your dependencies." Scout is convenient for dev-loop checks; for CI gating, Trivy/Grype's exit codes and JSON output are the more scriptable option.
CI/CD Integration
Jenkins
stage('Build & Scan') {
steps {
sh 'docker build -t myapp:${BUILD_NUMBER} .'
sh '''
trivy image myapp:${BUILD_NUMBER} \
--severity HIGH,CRITICAL \
--exit-code 1 \
--format json -o trivy-${BUILD_NUMBER}.json
'''
}
post {
always {
archiveArtifacts "trivy-*.json"
}
}
}
Archiving the JSON report matters — it becomes the audit trail for "was this CVE present when we shipped?" and lets you diff findings between builds.
GitHub Actions
- name: Build image
run: docker build -t myapp:${{ github.sha }} .
- name: Scan with Trivy
uses: aquasecurity/trivy-action@master
with:
image-ref: myapp:${{ github.sha }}
severity: CRITICAL,HIGH
exit-code: '1'
format: 'sarif'
output: 'trivy.sarif'
- name: Upload to GitHub Security
uses: github/codeql-action/upload-sarif@v3
with:
sarif_file: 'trivy.sarif'
The SARIF upload is the nice touch — findings land in GitHub's Security tab with severity filtering, PR annotations, and trend tracking, no dashboard to build.
GitLab CI
scan:
image: aquasec/trivy:latest
script:
- trivy image $IMAGE_TAG --severity HIGH,CRITICAL --exit-code 1
GitLab also ships container scanning as a built-in template (Container-Scanning.gitlab-ci.yml) — check if your tier includes it before adding Trivy manually; the managed version handles the plumbing.
Reducing Image Vulnerabilities
Scanning finds the problems; these patterns prevent most of them:
1. Minimal Base Images
# Before — full Ubuntu image, hundreds of packages
FROM ubuntu:22.04
RUN apt-get install -y nodejs npm
# After — minimal Alpine or distroless
FROM node:20-alpine
# or even better: FROM gcr.io/distroless/nodejs20
Vulnerability reduction: node:20 (~800 CVEs) → node:20-alpine (~20 CVEs) → distroless (~5 CVEs). Fewer packages = fewer CVEs = smaller attack surface and a smaller image to boot. Distroless goes furthest — no shell, no package manager, nothing for an attacker to run — at the cost of docker exec debugging.
2. Multi-Stage Builds
# Build stage — has dev tools (attack surface not in final image)
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Final stage — only production deps
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
USER node
EXPOSE 3000
CMD ["node", "dist/index.js"]
Compilers, headers, and test tooling live in the build stage and never reach production — a gcc binary in your runtime image is a tool an attacker would love to find.
3. Pin Versions
# Before — unpinned, updates break things
FROM node:latest
RUN npm install express
# After — pinned, reproducible
FROM node:20.18.0-alpine3.20
RUN npm install express@4.21.0
Pinning trades "silently stale" for "deliberately updated" — a pinned base rebuilds identically every time, and upgrades become a reviewed diff instead of a surprise.
4. Clean Package Cache
# apt-get cache adds ~50MB and stale package lists
RUN apt-get update && apt-get install -y curl \
&& rm -rf /var/lib/apt/lists/*
# apk --no-cache
RUN apk add --no-cache curl
The cache lives in a layer forever — rm in a separate RUN doesn't shrink the image, it has to be in the same instruction.
5. Remove Build Tools After Compiling
# gcc/make are needed for native modules but not at runtime
RUN apk add --no-cache --virtual .build gcc make python3 \
&& npm install \
&& apk del .build
Common Fixes
"Vulnerability found in os package"
# Update the base image — pull latest
docker pull node:20-alpine
docker build --no-cache -t myapp:latest .
Most OS-package CVEs are fixed upstream in the next base rebuild — --no-cache forces a fresh pull of everything, which is the point.
"Vulnerability found in npm package"
# Check which package has the CVE
trivy image myapp:latest --severity CRITICAL | grep "npm"
# Update in package.json, rebuild
Trivy's output names the installed package and the fixed version — the fix is usually a dependency bump, not a code change. Dependabot/Renovate automate this entirely for direct dependencies.
"Vulnerability found in golang binary"
# Rebuild with updated Go toolchain
docker build --build-arg GO_VERSION=1.23 -t myapp:latest .
Go binaries embed their dependencies — the fix is rebuilding with a patched toolchain or updated module, not patching the binary.
Scanning Strategy
PR opened ──► SAST + dep scan (fast, catch code issues)
Merge to main ──► Build + Trivy scan (catch image issues)
Deploy to staging ──► DAST scan (catch runtime issues)
Deploy to prod ──► Runtime monitoring (catch live issues)
Image scanning sits in the middle: after your code is built but before it ships. Order matters — scan the built image, not the source, because what you're shipping is the image.
Related Articles
Our tutorials and troubleshooting runbooks are written by a DevOps practitioner with hands-on experience operating Kubernetes, Terraform, CI/CD pipelines, and Linux infrastructure in production. Commands, manifests, and configurations are verified against current tooling releases. Learn more about our editorial process or report a correction or outdated flag.