Sub topic 1
What is inside a container image
This chapter explains how an image is assembled from layers and packages. That structure decides what a vulnerability scanner can see and report.
Prerequisites
This chapter assumes you can do the following:
- Pull, build, and run images with Docker.
- Use the command line.
- Read a basic Dockerfile.
- Describe a software dependency, meaning code your program uses but did not write.
Layers: how an image is built
A container image is a stack of read-only layers. Most Dockerfile instructions that change the filesystem, such as FROM, COPY, and RUN, add a layer. Each layer stores only the files that changed compared with the layer below it. The image also carries metadata, such as the default command and environment variables.
Layers matter for scanning because a package stays in the image once a layer has added it. A later RUN rm hides the files in the final filesystem view. The earlier layer still holds them, and a scanner that reads layers can still see them.
Where packages come from
Almost everything in an image arrives through one of three routes.
| Source | Example | Where a scanner finds it |
|---|---|---|
| Base image | `FROM python:3.12-slim` | The operating system release file and package database |
| OS package manager | `RUN apt-get install -y curl` | The package database, such as `/var/lib/dpkg/status` |
| Language package manager | `RUN pip install requests` | Metadata files, such as `site-packages/*.dist-info` or `package-lock.json` |
Your own code is usually a small part of the image. The base image and its dependencies often make up most of it, and you inherit their flaws.
Look inside an image
You can inspect the layers of any image you have pulled. This example uses python:3.12-slim.
docker pull python:3.12-slim
docker history python:3.12-slimdocker history lists each layer, the instruction that created it, and its size. You can also list the OS packages in the image without a scanner:
docker run --rm python:3.12-slim dpkg-query -W -f='${Package} ${Version}\n'The command prints one line per installed Debian package, with its name and version. Scanners read the same package database. They match each name and version against vulnerability data. The next chapter covers that data.
What a scanner can and cannot see
A scanner reports what it can identify from package metadata. This has consequences:
- Packages installed through a package manager are found reliably, because they are recorded in a database.
- Language dependencies are found when lockfiles or metadata directories are present.
- Software copied in as a bare binary, for example with
curl -oorCOPY, may leave no metadata. The scanner may miss it. - Files that a later layer deleted may still be reported, since they exist in an earlier layer.
Why this matters for the rest of the tutorial
Every fix in later chapters changes what is in the image. You can change the base image, remove packages you do not need, or build in stages so that compilers never reach the final layer. Knowing which layer brought in a package tells you which of those fixes applies.
Exercise
An engineer adds RUN apt-get install -y build-essential in one layer, then removes it with RUN apt-get remove -y build-essential in the next layer. Why might a layer-aware scanner still report vulnerabilities in those packages, and which technique avoids the problem?
Show answer
The first layer still contains the installed files, and an image only adds layers. Removing a package in a later layer hides it from the final view but does not delete it from the earlier layer. A multi-stage build avoids this, because the build tools exist only in a build stage and the final image never includes them.