Shrinking images

Intermediate · 17 min

Measure first, then cut: smaller bases, multi-stage builds, no package caches, no duplicate layers from chmod and chown, and static binaries on scratch.

Before thisMulti-stage buildsChoosing a base image

Raghav Dua · 17 min

Shrinking images

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Raghav DuaReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Measure before cutting: docker image ls for totals, docker history for per-step sizes, and dive to see which files each layer adds.
  • The base image usually dominates. python:3.14 is 416 MB compressed for amd64, python:3.14-slim 44 MB and distroless static under 1 MB.
  • A multi-stage build leaves compilers, headers and source behind, which is often the single biggest saving.
  • Package managers leave caches. Use apt-get --no-install-recommends and clear lists in the same RUN, pip --no-cache-dir or a cache mount, and npm ci --omit=dev.
  • Changing a file's mode or owner in a later RUN copies the whole file into a new layer. Set them while copying with COPY --chmod and --chown.
  • A static binary can run FROM scratch, but then you must supply CA certificates, time zone data and a non-root user yourself.

Why size is worth an afternoon

A smaller image pulls faster, so a new node or an autoscaled replica starts sooner. It costs less in registry storage and transfer. And every package you do not ship is one that cannot have a CVE, which matters when scanners flag hundreds of findings in tools your app never calls.

The devops lesson covered the first rule: a file deleted in a later layer still ships in the earlier one. This lesson is the rest of the toolbox, in the order it usually pays off.

Measure first

docker image ls notes                     # total size
docker history notes:1.4.2                # size added by each step
dive notes:1.4.2                          # browse each layer's files and wasted space

docker history tells you which step to look at. dive, currently v0.13.1, shows what that step wrote, and flags files that are added in one layer and overwritten or deleted in another. Fix the biggest layer first.

The base image

The base is usually most of the image. I summed the compressed layer sizes of some linux/amd64 images from their registry manifests:

Image Compressed
python:3.14 416.2 MB
python:3.14-slim 43.5 MB
python:3.14-alpine 17.8 MB
node:24 410.0 MB
node:24-slim 81.1 MB
node:24-alpine 62.5 MB
golang:1.25 305.4 MB
debian:trixie-slim 29.8 MB
gcr.io/distroless/python3-debian13 22.6 MB
gcr.io/distroless/base-debian13 9.5 MB
alpine:3.22 3.8 MB
gcr.io/distroless/static-debian13 0.9 MB

The full python and node tags are build environments, with compilers and headers. Switching to -slim alone cuts roughly 90% for Python. Alpine is smaller still but uses musl instead of glibc, which can turn a quick pip install of prebuilt wheels into a source compile. The trade-offs between these families are the subject of choosing a base image.

Leave the toolchain behind

A multi-stage build compiles with the 305 MB golang image and ships the binary on a base of a few megabytes. For interpreted languages the same idea applies: install dependencies with build tools in one stage, copy the virtual environment or node_modules into a slim stage.

Package manager leftovers

Each package manager has its own way to not leave things behind:

RUN apt-get update \
 && apt-get install -y --no-install-recommends ca-certificates curl \
 && rm -rf /var/lib/apt/lists/*
RUN pip install --no-cache-dir -r requirements.txt
RUN npm ci --omit=dev && npm cache clean --force

--no-install-recommends skips the “recommended” packages Debian pulls in by default, often dozens. The apt lists must be removed in the same RUN, or they are already committed. A cache mount is the alternative for pip and npm: the cache is reused across builds and never reaches a layer at all.

The duplicate-layer trap

Layers store whole files. When a later step changes only a file’s metadata, the storage driver still copies the whole file up into the new layer, as the overlayfs lesson explains. So this stores an 80 MB jar twice:

COPY app.jar /app/app.jar
RUN chmod 755 /app/app.jar && chown app:app /app/app.jar   # copies the jar again

Set mode and ownership while copying instead, in one layer:

COPY --chmod=755 --chown=app:app app.jar /app/app.jar

The same applies to RUN chown -R app /app after a large COPY. dive shows this kind of waste directly.

Static binaries and scratch

A statically linked binary needs no libraries, so it can run on scratch, the empty image. Go with CGO_ENABLED=0 and Rust with a musl target both produce one. Nothing else is there, though, and three things are commonly missed:

FROM golang:1.25 AS build
WORKDIR /src
COPY . .
RUN CGO_ENABLED=0 go build -o /out/notes ./cmd/notes

FROM scratch
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /usr/share/zoneinfo /usr/share/zoneinfo
COPY --from=build /out/notes /notes
USER 65532:65532
ENTRYPOINT ["/notes"]

Without the CA bundle every HTTPS call fails with an unknown-authority error. Without zoneinfo, time zone conversions fail. And with no /etc/passwd, a user name means nothing, so use a numeric USER. gcr.io/distroless/static-debian13 is the same idea with those three already included, for under a megabyte, which is why it is often the better choice. There is also no shell in either, so debugging means other tools.

When to stop

The last 10 MB rarely matter as much as the first 400. Past a slim or distroless base and a multi-stage build, spend effort on keeping the image reproducible and patched, not on shaving bytes. And keep the context small with .dockerignore, so nothing slips in through COPY . ..

About the video

The embed is Raghav Dua’s “3 ways to reduce the size of your docker images”, July 2024, 17 minutes. Its chapters cover multi-stage builds, layer optimization, building images from scratch, and other techniques. It uses its own example app, so its numbers differ from the table above. YouTube reports an English original audio track. Laurent Goderre’s talk in deeper, from Docker, looks at what “distroless” really means and where it stops helping.

What was run: every size in the table is a sum of compressed layer sizes from the linux/amd64 image manifest, fetched from Docker Hub and gcr.io with curl. Uncompressed sizes on disk are larger. The Dockerfiles were written from the Dockerfile reference and the building best practices page and were not built, and the copy-up behaviour is from the overlayfs storage driver page, checked 2026-11-28.

Where this fits

That ends the build groups. Next, what happens to the files a running container writes: the container filesystem is ephemeral.

Check yourself

  1. 01Your Dockerfile has COPY app.jar /app/ then RUN chmod 755 /app/app.jar. The jar is 80 MB. How big is the jar's footprint in the image, and how do you fix it?

    About 160 MB. Changing the mode in a new layer copies the whole file up into that layer, so it is stored twice. Use COPY --chmod=755 app.jar /app/ instead.

  2. 02A Go service built with CGO_ENABLED=0 runs FROM scratch but fails with x509: certificate signed by unknown authority on every outbound HTTPS call. Why?

    scratch has no CA certificate bundle. Copy /etc/ssl/certs/ca-certificates.crt from the build stage, or use distroless static, which includes it.

  3. 03Where do you start when an image is unexpectedly 1.2 GB?

    docker history shows which step added the size, and dive shows which files that layer contains. Fix the biggest layer first, usually the base image or a build toolchain that should be in another stage.

  4. 04Why might alpine not be the smallest good choice for a Python service with compiled dependencies?

    Alpine uses musl, so many wheels built for glibc do not apply and packages compile from source, which needs build tools and can end up larger or slower. A slim glibc base often wins.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark