Sub topic 6
Choosing a better base image with recommendations
This chapter covers the docker scout recommendations command. It assumes you can pull, build and run images, read a Dockerfile, and read the output of docker scout quickview and docker scout cves from earlier chapters. You should also know what a CVE and a fixed version are.
Why the base image matters most
Most vulnerabilities in a container image do not come from your code. They come from the packages in the base image named in your FROM line. A general-purpose base such as debian or ubuntu ships a shell, a package manager, compilers or libraries, and many utilities your application never calls. Each package is a possible source of CVEs.
Changing the base image is often the cheapest fix available. It can remove dozens of findings with one line of the Dockerfile. The difficulty is knowing which replacement is safe to use. docker scout recommendations helps with that question.
What the command does
Docker Scout looks at the base image of the image you give it and compares that base against other tags in the same repository and against alternatives. It then suggests changes that would lower the number of vulnerabilities.
docker scout recommendations nginx:latestThe command can work on a local image, an image in a registry, or an image you just built. For Scout to identify the base image, the image needs provenance or enough layer information to match it against known base images. If Scout cannot detect a base image, the command reports that it has nothing to recommend. This is common for images built FROM scratch and for images whose base layers were squashed or rebuilt.
Reading the output
The output has two parts. The first describes your current base image. The second lists the suggested targets.
The current base section usually shows:
- The base image name and tag.
- Whether the tag is the newest one available.
- The number of vulnerabilities, grouped by severity.
- The size of the image.
The suggestions section is split into two kinds of recommendation:
| Kind | What it suggests | Typical effect |
|---|---|---|
| Refresh base image | A newer build of the same tag you already use, for example the latest patch of `node:20`. | Picks up fixes already published. Low risk. |
| Change base image | A different tag or variant, for example moving from a full variant to a `-slim` or `-alpine` variant, or to a newer major version. | Can remove many more findings. Higher risk, because behavior can change. |
Each suggestion shows the vulnerability counts it would leave and the difference from your current image. Use these numbers to compare options. A suggestion that removes many findings but changes the major version needs more testing than a refresh of the tag you already use.
A worked decision
Suppose you build an image from python:3.12 and a scan reports many findings in operating system packages. You run:
docker scout recommendations myapp:1.0Scout might propose two options: a refreshed python:3.12, and python:3.12-slim. Work through them in this order.
- Check the refresh first. It keeps the same tag and variant, so it is the lowest-risk change. Rebuild with
--pullso Docker fetches the newest base instead of reusing a cached one. - Check the change option next. Slim variants omit many packages, which removes their CVEs. They also omit build tools. If your Dockerfile runs
pip installfor a package that compiles C code, the build can fail. - Rebuild, then scan again with
docker scout quickviewto confirm the numbers dropped. - Run your tests against the new image before you ship it.
docker build --pull -t myapp:1.1 .
docker scout quickview myapp:1.1The --pull flag tells Docker to check the registry for a newer version of every image named in FROM. Without it, Docker can reuse an old local copy and you will see no improvement.
Updating the Dockerfile
After choosing a target, edit the FROM line. Here is a before and after for a Python service.
# Before
FROM python:3.12
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]# After
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "main.py"]Only the tag changed. If a dependency needs a compiler, add the specific packages you need with the package manager, or move the build into a separate stage. The next chapters cover multi-stage builds in more detail.
Edge cases and gotchas
Fewer findings does not mean a safer choice in every case
A smaller base can be a different operating system family. Alpine uses musl instead of glibc. Some compiled Python wheels, Node native modules and proprietary binaries assume glibc and fail or run slowly on Alpine. A recommendation is based on vulnerability counts and image metadata. It cannot know whether your application runs correctly on the new base. Treat each suggestion as a candidate, not an instruction.
Pinned digests hide updates
If your Dockerfile pins a base by digest, for example FROM python:3.12-slim@sha256:..., then --pull will never move you to a newer build. That is the purpose of pinning, and it gives reproducible builds. You must update the digest yourself when you want fixes. Scout's refresh recommendation tells you when a newer build exists, and then you change the digest by hand or with a dependency update tool.
The recommendation reflects the database at that moment
Vulnerability data changes daily. A tag that looks clean today can show new findings next week. Run recommendations again when you revisit an image, and do not rely on an old result.
The base may not be the source of the findings
Recommendations only address the base image layers. If most findings sit in your own application layers, such as a vulnerable library installed by pip or npm, changing the base will not fix them. Use docker scout cves to see which layer each finding comes from, and update the dependency instead.
Official images versus hardened images
Scout may suggest minimal or hardened base images when they exist for your language. Check who maintains them and how often they publish updates. A base with zero findings today is only useful if it keeps receiving security patches.
Combining with other commands
Recommendations fit into a short loop with the commands you already know.
docker scout quickview myapp:1.0
docker scout recommendations myapp:1.0
docker build --pull -t myapp:1.1 .
docker scout compare --to myapp:1.0 myapp:1.1The first command shows the starting point. The second suggests changes. The build applies one. The compare step, covered in the next chapter, shows exactly which vulnerabilities were removed and whether any new ones appeared.
The diagram shows this loop.
Common mistakes
- Rebuilding without
--pull. Docker reuses a cached base and nothing changes. - Switching variants without testing. A slim or Alpine base can break builds and runtime behavior.
- Judging only by total count. One critical finding with a fix matters more than ten low findings with none. Compare by severity.
- Expecting recommendations for every image. Images built from
scratch, or without detectable base information, get no suggestions. - Forgetting to rescan. Always confirm the result with a second scan after the rebuild.
Summary of the decision process
- Run recommendations to see whether your base image is current.
- Prefer a refresh of the same tag when it removes enough findings.
- Consider a variant change when you need a larger reduction, and test it.
- Rebuild with
--pull, then rescan and compare. - If findings remain in your own layers, fix the dependencies instead.
Exercise
You run docker scout recommendations myapp:1.0 and Scout suggests both a refresh of node:20 and a change to node:20-alpine. You rebuild with the refreshed base and the vulnerability count does not drop. Give two possible reasons.
Show answer
First, the Dockerfile may pin the base by digest, or the build may have reused a cached base because --pull was not used, so the new base was never fetched. Second, most of the findings may come from application layers, such as packages installed by npm, which a base image refresh does not change. Check the build command and use docker scout cves to see which layers hold the findings.
Exercise
Why should you test your application before adopting a recommended change to a -slim or Alpine variant, even if Scout shows far fewer vulnerabilities?
Show answer
Scout bases its suggestion on vulnerability counts and image metadata, not on whether your application works. Slim and Alpine variants remove packages and can use a different C library (musl instead of glibc), so builds that need compilers or binaries that expect glibc can fail or behave differently.