Sub topic 1
What is inside a container image
A vulnerability scanner does not run your container or probe it over the network. It reads the image, builds a list of the software it finds, and compares that list with a vulnerability database. This chapter explains what the scanner reads, so you can predict what it will report and why.
Prerequisites
This chapter assumes you can:
- Pull, build and run images with Docker.
- Use the command line, including pipes and
head. - Read a basic Dockerfile.
- Explain what a software dependency is, for example a library your code imports.
You need Docker installed. The examples use public images (python:3.12-slim and alpine:3.20), so no account is required.
An image is a stack of layers
A container image is a set of files plus metadata. The files are stored as an ordered list of layers. Each layer is a tar archive that records the files added, changed or deleted by one build step. When a container starts, Docker stacks the layers into one filesystem view.
Three parts make up an image:
| Part | What it holds | Why a scanner cares |
|---|---|---|
| Manifest | The list of layers and the config, each identified by a SHA-256 digest | Identifies exactly which content is being scanned |
| Config | Environment variables, entrypoint, build history, and the ordered layer digests | Shows the OS and how the image was built |
| Layers | The filesystem contents as tar archives | The source of the package inventory |
You can view the layer digests of any local image:
docker pull python:3.12-slim
docker image inspect --format '{{json .RootFS.Layers}}' python:3.12-slimTo see the build step that created each layer, use docker history:
docker history --no-trunc python:3.12-slimEach row is one instruction from the image's Dockerfile. Rows that only change metadata, such as ENV or CMD, create no files and show a size of 0B.
Layers are immutable and shared. If two images start from the same base image, they share the same base layers on disk. Scanners take advantage of this: a vulnerable base layer produces the same findings in every image built on it.
Where packages are recorded
A scanner needs a list of names and versions. It does not guess from file names. It reads the records that package managers leave behind. Which record exists depends on the base image's Linux distribution.
| Distribution family | Package manager | Database file inside the image |
|---|---|---|
| Debian, Ubuntu | `apt` and `dpkg` | `/var/lib/dpkg/status` |
| Alpine | `apk` | `/lib/apk/db/installed` |
| Red Hat, Fedora, Amazon Linux | `dnf`, `yum`, `rpm` | RPM database under `/var/lib/rpm` (newer releases use `/usr/lib/sysimage/rpm`) |
Look at these files yourself. This shows the first lines of the Debian-style database in the Python image:
docker run --rm python:3.12-slim sh -c 'head -n 12 /var/lib/dpkg/status'And this lists the first Alpine entries:
docker run --rm alpine:3.20 sh -c 'head -n 12 /lib/apk/db/installed'In the dpkg output you will see fields such as Package, Version and Architecture. Those fields are what the scanner matches against advisory data.
You can also ask the package manager itself for a compact list:
docker run --rm python:3.12-slim dpkg-query -W -f '${Package} ${Version}\n'A typical slim Debian-based image lists about a hundred packages, such as libc6, openssl and zlib1g. Most vulnerability findings in a typical image are in these operating system packages, not in your application code.
Language dependencies
Operating system packages are only part of the picture. Application dependencies are recorded in language-specific files, and scanners look for those too:
| Ecosystem | Files commonly read |
|---|---|
| Python | Installed package metadata under `site-packages` (`*.dist-info`), sometimes `requirements.txt` or `poetry.lock` |
| Node.js | `package-lock.json`, `yarn.lock`, installed `node_modules/*/package.json` |
| Java | `pom.xml`, JAR files with embedded metadata |
| Go | Module information embedded in compiled binaries |
| Ruby | `Gemfile.lock` |
| Rust | `Cargo.lock`, or metadata embedded in binaries built with auditable tooling |
Check what Python packages a container has installed:
docker run --rm python:3.12-slim pip listEven a clean Python image includes pip, and often setuptools. These are real dependencies and they can carry their own vulnerability reports, separate from the Debian packages beneath them.
Why the contents differ from what you expect
Reading these records explains most surprises in scan results.
Deleted files remain in lower layers
A later layer can hide a file but cannot remove it from an earlier layer. Consider this Dockerfile:
FROM debian:12-slim
RUN apt-get update && apt-get install -y curl build-essential
RUN apt-get purge -y build-essential && apt-get autoremove -yThe second RUN records deletions in a new layer. The files from the first RUN still exist in the earlier layer, and they still add to the image size. Scanners generally work from the final merged filesystem, and the package database reflects the purge. Still, any secret or file you copied and deleted in a later step remains extractable from the layer. The reliable fix is to install and remove in the same RUN instruction, or to use a multi-stage build (covered in a later chapter).
Software that bypasses the package manager
If you download a binary with curl and place it in /usr/local/bin, no package database records it. A scanner that relies on database records will not see it. Some scanners also recognize well-known binaries, but you should not count on that. The same applies to files copied with COPY and compiled-in libraries.
Statically linked and compiled applications
A Go or Rust binary contains its dependencies inside the executable. Some scanners can read embedded module metadata. Others cannot. A C program that statically links a library records nothing at all.
Minimal images have fewer records
Distroless and scratch images contain little or no package manager. Distroless images keep individual package records in /var/lib/dpkg/status.d/ so scanners can still identify the contents. A scratch image holds only what you copy in, so the only findings come from your own files.
Base image versus your additions
The final image combines two sources of packages. The base image supplies the operating system and anything pre-installed. Your Dockerfile adds more. When a scan shows 80 findings, the first question is which layer introduced them.
A quick way to reason about this is to scan or list the base image alone, then compare against your image. The package lists are different, so the findings are different. Later chapters use docker scout compare and trivy image to do this properly.
You can also see the total number of layers and approximate size by step:
docker history --format 'table {{.CreatedBy}}\t{{.Size}}' python:3.12-slimLarge layers near the top of the history, from the base, tend to correspond to the operating system. Small layers at the bottom come from your own instructions.
Common errors
- Scanning a stale local copy. A tag such as
latestmoves.docker pullfirst, or scan by digest, so the result describes the image you ship. - Assuming a small image is a clean image. Size and vulnerability count are related but not the same. A small Alpine image can still contain a vulnerable
openssl. - Expecting the scanner to read your source code. Scanners match package versions to advisories. They do not analyze your own logic for bugs.
- Forgetting the config. The image config may contain secrets in environment variables. Package scanners do not look for those unless you enable secret scanning.
Summary of what to remember
- An image is a manifest, a config, and a stack of immutable layers.
- Scanners rebuild a package list from OS package databases and from language manifests and lock files.
- Software installed outside a package manager, or compiled into a binary, may be invisible to the scanner.
- Deleted files stay in earlier layers, so build steps that install and clean up in one instruction are safer.
- Findings come from both the base image and your additions.
Exercise
You build an image that downloads a tarball with curl, unpacks a vulnerable library into /opt/lib, and uses no package manager for it. A scan reports nothing for that library. Give two reasons this can happen and one way to make the library visible to scanners.
Show answer
The scanner builds its list from package manager databases and language lock files, and /opt/lib appears in neither. Scanners also do not generally recognize arbitrary files by name. To make it visible, install the library through the package manager or a language dependency manager that leaves a database entry or lock file, for example apt or pip. You can also provide an SBOM that lists it. Another valid reason is that it is compiled or copied in a form the scanner does not parse.
Exercise
Which command prints the installed package names and versions inside a Debian-based image, and which file does the scanner read to get the same information?
Show answer
Run docker run --rm python:3.12-slim dpkg-query -W -f '${Package} ${Version}\n'. Scanners read /var/lib/dpkg/status, the dpkg database, from the image layers.