Container image scanning
An old base image, a current one, and a gate on criticals.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-gitlab/imagescan.tar.gz && tar -xzf imagescan.tar.gz, which creates ~/lab/imagescan/. SHA-256: 32afeb5867ff4964cac5f47edd161cd3dca89f3a92cd6b96fbfb9f57fca03a5aYour container image ships your code and a whole Linux distribution under it: a C library, OpenSSL, compression libraries, a shell. Most of the vulnerabilities in a typical image are in that base layer, which nobody on the team wrote or reads. Container scanning lists the packages in an image and checks each against vulnerability data. GitLab's container scanning template runs on the Free tier, and like the other templates it reports without failing anything.
The template
# GitLab's container scanning template (Trivy inside), pointed at an image by digest.# CS_IMAGE is the image to scan; in a real project it is the image this pipeline just built.include:- template: Jobs/Container-Scanning.gitlab-ci.ymlstages: [test, gate]variables:CS_IMAGE: python:3.10.0-slim@sha256:ad540a471260fee5e5e1a99ee2acf142efe8c279a7a54315160d8033ba88f0d8image-gate:stage: gateimage: secopslog-py:3.15needs:- job: container_scanningartifacts: truescript:- python gate.py gl-container-scanning-report.json Critical | tail -3- python gate.py gl-container-scanning-report.json Critical > /dev/null
CS_IMAGE names the image to scan. In a real project it's the image the pipeline just built and pushed. Here it's an old Python base image, pinned by digest so the scan always looks at the same bytes. image-gate is the gate.py from the SAST lesson, failing on any Critical. Treat it as a starting point: the hands-on shows why a production gate counts only Criticals that have a fix. In production, scan the image you built by its digest, and deploy that same digest, so the scanned bytes are the shipped bytes.
The template runs GitLab's container scanning analyzer, which uses Trivy inside, from the tag container-scanning:8, a major version that moves with every release, as the SAST analyzer did. CS_IMAGE is our pinned image. The job also writes a CycloneDX SBOM of the image next to its report.
An old base image
748 findings in a base image nobody on the team wrote a line of: 31 Critical, 228 High, and hundreds more. The gate failed on the 31 Criticals. tail -3 shows only the end of the list, here two High findings in zlib1g, the compression library, one of dozens of packages that came with the base image. The full list is in the job log and the report. Python 3.10.0 slim is from 2021, and its Debian packages have been fixed many times since.
A current base image
The quickest fix for base-layer findings is usually a newer base image. Scan the current python:3.15-slim, again by digest, by overriding CS_IMAGE for one pipeline:
No Criticals, and the gate passed. The current image still has 189 findings, 49 of them High. Many of them are in Debian packages with no fixed version released yet. For those, a newer image doesn't help today, and a gate that failed on them would block every pipeline with nothing anyone could do about it.
| Gate | Effect |
|---|---|
| Fail on every finding | Fails on almost every image, so it gets switched off |
| Fail on Critical | Catches the worst, but can still fail on things nobody can fix yet |
| Fail on Critical with a fix available | Fails only when an upgrade would help. Track the rest |
| No gate, read the report | Findings pile up unread |
Hands-on
Take the report of the first scan, the old image. It belongs to the older of the two container_scanning jobs, so pick it with sort_by(.id)[0] instead of the [0] the counts step used, and for each severity count how many findings have a fix available. In GitLab's report format, a finding with a fix has a non-empty solution. How many Criticals would a "Critical with a fix available" gate fail on? Finish with ./down.sh.
What you should see: in the old image, 26 of the 31 Criticals have a fix available, and so do 103 of the 228 Highs. A "Critical with a fix available" gate would fail on those 26, every one solved by upgrading a package or the base image. The other five are known and unfixed, worth tracking, and nobody can fix them yet.
Remember
Scan the image you ship, gate on Critical findings that have a fix, rebuild on current base images, and rescan running images on a schedule, because a scan only reflects the day it ran.
solution.CS_IMAGE, not on itself.