Sub topic 1
What a base image gives your container
Prerequisites
This chapter assumes you can build and run an image with docker build and docker run. You should be able to read a simple Dockerfile. You should also know what a package manager and a shared library are.
What FROM actually does
A base image is the starting filesystem and metadata for your image. The FROM instruction pulls it, and every later instruction adds a layer on top. Whatever the base contains ships in your final image, whether your app uses it or not.
A base image typically provides:
- A root filesystem. Directories such as
/etc,/usr, and/lib, plus the files in them. - A C library. Most compiled programs and language runtimes load it at start-up. The next chapter covers glibc and musl.
- Userland tools. These can include a shell,
ls,cat, and a package manager such asaptorapk. - CA certificates and basic configuration. Examples are
/etc/passwdand the files that control name resolution. - Image metadata. This covers the default user, environment variables, and the default
CMDorENTRYPOINT.
The base image does not provide a kernel. A container shares the kernel of the host.
How layers add up
Your final image is the base image's layers plus the layers from your own instructions. The diagram shows this for a Python app.
Everything in the base layers counts toward image size, pull time, and the list of packages a vulnerability scanner reports.
A worked example
Here is a minimal Dockerfile. The base image is the first line, and it decides most of what the image contains.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]python:3.12-slim is a Debian-based image with the Python interpreter already installed. Because it is Debian-based, it uses glibc. Most prebuilt Python wheels target glibc, so pip install usually works without a compiler.
You can inspect what a base gives you without writing a Dockerfile. These commands check for a shell and a package manager:
docker run --rm python:3.12-slim sh -c "ls /bin | head -5; which apt-get"
docker image ls pythonThe first command works because the image includes sh and apt-get. The same check fails on an image that has no shell. That difference is the main reason to pick one base over another.
Why the choice matters
Four properties follow from the base image.
| Property | What the base image decides |
|---|---|
| Compatibility | Which C library is present, and so which prebuilt binaries run |
| Size | How much each pull, push, and deploy transfers |
| Attack surface | How many packages and tools an attacker or a scanner can find |
| Debuggability | Whether you can open a shell inside the running container |
These properties pull against each other. Smaller images usually have fewer vulnerabilities and fewer tools. Fewer tools make debugging harder, and a different C library can break software that worked on your laptop.
The rest of this tutorial compares four families against these four properties: Debian slim, Alpine, distroless, and Docker Hardened Images.
Common mistakes
- Choosing by size alone. A small image that fails on a missing library costs more time than it saves.
- Using
latest. The tag moves, so two builds of the same Dockerfile can produce different images. A later chapter covers pinning. - Assuming a shell exists. Scripts in
CMDor health checks fail on images that have nosh.
Exercise
You build an image from FROM python:3.12-slim, then add a 300 MB compiler toolchain with RUN apt-get install. Which part of the final image's size and scan results comes from the base image, and which part from your own layers?
Show answer
The base image supplies the Debian slim files, the C library, and the Python runtime. The compiler toolchain is a layer you added, so its size and any vulnerabilities in its packages are yours. A scanner reports both together because the final image contains both sets of layers.
Exercise
Name two properties of a container that are decided by the base image, and say why a container's kernel is not one of them.
Show answer
Any two of compatibility (C library), size, attack surface, or debuggability (shell and tools). The kernel is not part of any image because containers share the host's kernel.