Sub topic 7
Comparing image versions with docker scout compare
This chapter covers docker scout compare. It shows what changed in vulnerabilities and packages between two versions of an image. It assumes you can already run docker scout cves and read its output, and that you know what a CVE, a severity rating and a fixed version are. It also assumes you can build and tag images with a Dockerfile.
Why compare two versions
A single scan answers "what is wrong with this image today?" That list can be long, and most of it may have been there for months. When you ship a new version, the more useful question is "what did this change do to my risk?"
docker scout compare answers that question. It analyzes two images and reports the difference between them:
- Vulnerabilities that exist only in the new image (added).
- Vulnerabilities that exist only in the old image (removed).
- Vulnerabilities present in both (unchanged).
- Packages that were added, removed, or changed version.
This fits several common situations:
- You bumped a base image tag and want to confirm it helped.
- You upgraded a dependency and want to be sure it did not pull in something worse.
- You are reviewing a pull request and want a quick view of the security impact.
- You want to show that a remediation actually removed the findings it was meant to remove.
The basic command
The comparison uses a baseline and a target. The baseline is the image you compare against. The target is the image you are evaluating. The --to option names the baseline, and the positional argument names the target.
docker scout compare --to myapp:1.0 myapp:2.0Read this as "compare myapp:2.0 to myapp:1.0". The version 1.0 image is the reference point. Everything in the output is described from the point of view of 2.0: what 2.0 added, what it removed, and what it kept.
The images can be local, or they can come from a registry. Docker Scout uses the local image store first when the reference matches a local image. If you build myapp:2.0 locally and have not pushed it, the comparison still works.
If you leave out the target, Docker Scout uses the most recent image in your local context. In scripts, always name both images so the result does not depend on what you built last.
A worked example
Suppose you have a small Python service. Version 1.0 was built on an older base image. Version 2.0 moves to a newer one.
# Dockerfile for myapp:1.0
FROM python:3.10-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]For 2.0, only the first line changes:
# Dockerfile for myapp:2.0
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]Build both and compare them:
docker build -t myapp:1.0 -f Dockerfile.v1 .
docker build -t myapp:2.0 -f Dockerfile.v2 .
docker scout compare --to myapp:1.0 myapp:2.0The output has several sections. The exact layout varies between Docker Scout versions, but the content follows this shape:
- A header that names the target and the baseline, with their digests and platforms.
- A vulnerability summary for each image, broken down by severity (critical, high, medium, low, unspecified).
- A section for the base image of each, when Docker Scout can identify it.
- A list of changed packages, with old and new versions.
- A list of vulnerabilities that differ, marked as added or removed.
Treat the numbers as a delta. If the baseline has 12 high findings and the target has 4, the base image upgrade removed 8 high findings, assuming none were added. Check the added list separately, because a net decrease can hide a new problem.
How the comparison works internally
Docker Scout does not scan the two images and subtract the totals. It works from each image's package inventory, which is its SBOM (the list of installed packages and versions covered earlier in this tutorial). It then matches each package against advisory data and compares the resulting sets of findings.
Two consequences follow from this design.
First, a vulnerability can appear as "removed" for two different reasons. The package may have been upgraded to a fixed version, or the package may have been dropped from the image entirely. Both reduce risk, but they mean different things for you. The package list in the output tells you which one happened.
Second, a vulnerability can appear as "added" even though you did not change anything it touches. If a new CVE is published between your two scans, or if the advisory data is updated, the same package version can produce a new finding. Docker Scout compares against current advisory data for both images, so this affects both sides equally. A finding that is new in the data appears in both lists and counts as unchanged, not added. An added finding therefore points to a real difference in the image contents.
Limiting the output
On a large image, the full comparison is long. Several options narrow it down.
# Only show critical and high findings
docker scout compare --to myapp:1.0 --only-severity critical,high myapp:2.0
# Ignore findings that have no fixed version yet
docker scout compare --to myapp:1.0 --ignore-unchanged myapp:2.0The options you will use most:
| Option | Effect |
|---|---|
| `--to` | Sets the baseline image to compare against. |
| `--only-severity` | Keeps only the listed severities, such as `critical,high`. |
| `--ignore-unchanged` | Hides vulnerabilities and packages that are the same in both images, so only differences remain. |
| `--ignore-base` | Leaves out the base image layers, so you see only what your own layers contributed. |
| `--format` | Selects the output format, for example `json` for other tools to read. |
| `--exit-on` | Makes the command return a failing exit code under a condition, such as new vulnerabilities. |
Run docker scout compare --help to confirm the options your installed version supports. Docker Scout is updated frequently, and options are occasionally added or renamed.
Separating base image changes from your own
When an image changes, the cause is usually one of two things: the base image moved, or your application layers changed. The two call for different fixes.
Use --ignore-base to focus on your own layers:
docker scout compare --to myapp:1.0 --ignore-base --ignore-unchanged myapp:2.0If the result is empty with --ignore-base but large without it, all of the change came from the base image. If the result is large with --ignore-base, your own dependencies changed. This tells you where to look when a new vulnerability shows up.
Comparing against what is running or published
The baseline does not have to be a version you built yourself. Common baselines include:
- The image currently deployed, so you see what a release would change in production.
- The
latesttag of the same repository in a registry. - The previous release tag.
docker scout compare --to registry.example.com/team/myapp:latest myapp:2.0Docker Scout also supports comparing to an environment, if you have recorded your deployments as named environments such as production. That setup is outside this chapter. The practical point is that the baseline should be whatever you are replacing.
Using compare as a gate
Because compare reports differences, it makes a good gate for pull requests. A full scan gate fails on any high finding, including ones your team has already accepted. A compare gate fails only when a change makes things worse.
The --exit-on option controls this:
docker scout compare --to myapp:1.0 --only-severity critical,high --exit-on vulnerability myapp:2.0With --exit-on vulnerability, the command exits with a non-zero status if the target has vulnerabilities the baseline does not, within the severities you selected. Check docker scout compare --help for the accepted values in your version, since the available conditions can change. Chapter 14 builds this into a complete CI step.
Common mistakes
- Swapping the two images. If
--tonames the new image and the argument names the old one, "added" and "removed" are reversed. A good fix looks like a regression. Say the command aloud as "compare target to baseline" before you trust the output. - Comparing different platforms. An image built for
linux/amd64and one built forlinux/arm64can have different package sets. Pass--platformto both when you build multi-platform images, so you compare like with like. - Trusting totals alone. A drop from 10 findings to 8 could be 5 removed and 3 added. Read the added list.
- Comparing moving tags.
latestcan change between your two runs. Compare by digest when you need a reproducible result. - Ignoring non-vulnerability changes. The package diff also shows packages that grew in version without any CVE change. These are worth a glance, because large unexpected changes may mean your build pulled something you did not intend.
Reading a result: a short checklist
- Confirm which image is the baseline and which is the target.
- Check the added list first. These are the regressions.
- Check the removed list and confirm the reason, either an upgrade or a package that was dropped.
- Use
--ignore-baseto find whether the change came from your layers or the base image. - Decide whether the net result justifies shipping, and record any accepted findings as covered in the triage chapter.
Exercise
You run docker scout compare --to myapp:1.0 myapp:2.0. The summary shows high findings falling from 9 to 6. What must you still check before concluding that 2.0 is safer, and which option hides findings common to both images?
Show answer
Check the added list, because the net drop of 3 could include newly added findings alongside a larger number of removed ones. You should also confirm the baseline and target were not swapped. The --ignore-unchanged option hides vulnerabilities and packages that are the same in both images, so only the differences remain.
Exercise
A compare run with --ignore-base shows no differences, but the run without it shows 14 removed vulnerabilities. Where did the improvement come from, and what does that tell you about your own dependencies?
Show answer
The improvement came entirely from the base image, for example from moving to a newer base tag. The empty --ignore-base result means your own layers and dependencies contributed no vulnerability changes between the two versions.