Sub topic 14
Scanning images for vulnerabilities
This chapter assumes you can read a GitHub Actions workflow, know how docker/build-push-action works, and understand tags from the earlier chapters.
What a scanner checks
A vulnerability scanner reads the list of packages inside an image. It covers operating system packages such as apk or dpkg entries, and language dependencies such as package-lock.json. It compares each package version against a database of known CVEs (publicly catalogued security flaws). Each finding has a severity: LOW, MEDIUM, HIGH or CRITICAL.
A scanner only finds known flaws in identified packages. A clean report does not mean the image is safe.
Where the scan goes
Scan before you push. If the scan fails, the bad image never reaches the registry. The pipeline builds the image, loads it into the runner's local Docker daemon, scans it, and pushes only if the scan passes.
A scan step with Trivy
This workflow uses Trivy, an open-source scanner. The first build uses load: true so the image exists locally and push: false. The second build reuses the cache from the first, so it adds little time.
name: build-scan-push
on:
push:
branches: [main]
permissions:
contents: read
packages: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- name: Build for scanning
uses: docker/build-push-action@v6
with:
context: .
load: true
push: false
tags: scan-target:ci
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Scan image
uses: aquasecurity/[email protected]
with:
image-ref: scan-target:ci
severity: HIGH,CRITICAL
ignore-unfixed: true
exit-code: "1"
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Push
uses: docker/build-push-action@v6
with:
context: .
push: true
tags: ghcr.io/${{ github.repository }}:${{ github.sha }}
cache-from: type=ghaThe settings that matter:
exit-code: "1"makes the step fail when findings match the filter. The default is0, which reports findings but never blocks.severitylimits which findings count. Gating onLOWandMEDIUMusually blocks every build.ignore-unfixed: trueskips CVEs with no available patch, since you cannot act on them.
Common errors
- Scanner step passes but never blocks. The
exit-codeinput was left at its default of0. - Scanning the wrong image. The scan must target the same build you push. Scanning an older tag tests nothing useful.
- A new CVE breaks an unchanged image. The vulnerability database updates daily, so yesterday's passing image can fail today. Run the same scan on a schedule against published images too.
- A flaw in the base image. Rebuilding picks up a newer base image only if the cache is not stale. Rebuild periodically without cache.
Exercise
A scan step reports CRITICAL findings, but the workflow still pushes the image. Name two likely causes.
Show answer
The scan step's exit code is 0, because exit-code was not set to 1. Or the push step does not depend on the scan step passing, for example it runs in a separate job without needs, or it uses if: always().