Optimisation for Dockers
Docker image optimisation is the practice of making images smaller, faster to build and predictable. The main tool is the multi-stage build, where one stage compiles code and a later stage copies only the finished artefact into a slim base, along with pinned versions for reproducible builds.
Stage: Building Images · Part of the Docker roadmap · Last reviewed 19 November 2026
What to learn in Optimisation
- Write a multi-stage Dockerfile that builds in one stage and ships from another
- Pick a slim or distroless base image and weigh it against debugging convenience
- Combine RUN commands and clean package caches to avoid bloated layers
- Pin base image digests and dependency versions so rebuilds produce the same result
- Use BuildKit cache mounts to speed up repeated dependency downloads
Optimisation interview questions
How does a multi-stage build reduce image size?
Each FROM starts a new stage. You compile in a stage that has compilers and dev tools, then use COPY --from to take only the built binary or bundle into a small final stage. Build tools and source never reach the shipped image.
Why does deleting a file in a later RUN not shrink the image?
Layers are additive and immutable. A file written in an earlier layer stays stored there, and a later delete only hides it in the merged view. To avoid the size, download, use and remove the file within the same RUN instruction, or use a multi-stage build.
Where Optimisation fits in the Docker roadmap
Optimisation belongs to the Building Images stage (beginner level), which takes about 12 hours. Dockerfiles.
Other units in this stage
Back to the full Docker roadmap