Sub topic 4
Debian slim: the compatible default
This chapter assumes you can build and run Docker images, read a simple Dockerfile, and know what a shared library and a package manager are. It covers what Debian slim images contain, why they are a safe first choice, and where they cost you.
What "slim" means
Debian slim images are variants of the official Debian-based images. The python:3.12-slim tag is built on Debian and ships the language runtime plus a small set of packages. It leaves out many things the full image includes, such as compilers, development headers, and extra command-line tools.
The key property is the C library. Debian uses glibc, the same C library used by most mainstream Linux distributions. Most software is built and tested against glibc, so prebuilt binaries usually just work.
Why it is the compatible default
Python wheels (prebuilt packages) are published for glibc Linux as manylinux wheels. On a glibc base, pip install downloads a wheel and finishes without a compiler. The same applies to many Node native modules and to precompiled binaries from vendors.
The base keeps apt, a shell, and standard tools. You can install a missing package and open a shell to inspect a problem. This makes slim images the easiest to debug of the minimal-ish options.
FROM python:3.12-slim
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
COPY . .
CMD ["python", "app.py"]Copying requirements.txt before the rest of the source lets Docker reuse the cached dependency layer when only your code changes. --no-cache-dir stops pip from storing downloaded files in the image.
Installing system packages
Slim images do not include everything. If a package needs a system library, install it with apt-get in a single layer and remove the package lists afterward.
FROM python:3.12-slim
RUN apt-get update \
&& apt-get install -y --no-install-recommends libpq5 \
&& rm -rf /var/lib/apt/lists/*Running apt-get update and install in the same RUN avoids a stale cached package index. --no-install-recommends skips optional packages that would grow the image.
Trade-offs
Slim is smaller than the full Debian-based image but larger than Alpine or distroless. It still contains a shell, apt, and a base userland, so there are more packages that can show up in a vulnerability scan.
| Property | Debian slim |
|---|---|
| C library | glibc |
| Package manager | apt |
| Shell | Yes |
| Prebuilt wheels and binaries | Usually work |
| Size | Moderate |
| Attack surface | Larger than distroless |
You can check the cost yourself by comparing sizes with docker image ls and scanning with docker scout cves, which later chapters cover.
Where slim fits in the decision
The usual pattern is to start with slim and move to something smaller only when you have a reason, such as a security policy or a size limit. Because slim is glibc-based, the move to distroless Debian images later is smooth: the same binaries run against the same C library.
Exercise
A Python service installs a package that ships only a glibc manylinux wheel. Why does pip install succeed on python:3.12-slim without a compiler, and why should you run apt-get update and apt-get install in the same RUN line?
Show answer
The slim image uses glibc, so pip can use the prebuilt manylinux wheel and does not need to compile anything. Running the two apt commands in one RUN prevents Docker from reusing a cached, outdated package index from an earlier apt-get update layer.