Three techniques to shrink a Docker image
practical image-size reduction with trade-offs.
multi-stage builds to drop build tooling, smaller base images like slim or distroless, and fewer or cleaner layers plus dockerignore.
What's really being asked
This checks whether you know concrete size-reduction levers and can reason about what each costs in debuggability or build complexity.
The full answer
First, multi-stage builds: compile or install in a builder stage, then COPY only the runtime artifact into a minimal final stage, leaving compilers, headers, and dev dependencies behind. Trade-off is a slightly more complex Dockerfile. Second, a smaller base image: moving from a full OS image to slim, alpine, or distroless can cut hundreds of megabytes. Alpine uses musl libc which can break glibc-dependent binaries and complicate native modules; distroless has no shell or package manager, so debugging requires ephemeral containers or a debug variant. Third, layer hygiene: combine related commands, clean apt or package caches within the same RUN so they never persist in a layer, pin and remove only what is needed, and add a dockerignore so build context junk never enters the image.
The mistakes people make
Listing techniques without trade-offs. Suggesting docker save or compression as a real fix. Cleaning caches in a separate RUN, which leaves the bloat in the earlier layer. Switching to alpine without considering musl breakage.
What usually comes next
Why does cleaning in a later layer not help? When is alpine risky? How do you debug a distroless image? How does squashing differ from multi-stage?
A concrete example
A 2GB Node image carried the full build toolchain. A multi-stage build compiles in a node builder, then copies node_modules and the dist folder into a node-slim runtime stage, dropping compilers. Switching the runtime base from the full image to slim removes hundreds of megabytes more, and a dockerignore excludes the local node_modules and git history. The result drops well under 300MB, at the cost of a two-stage Dockerfile and fewer debugging tools in the final image.
Interview question
Why does running apt-get clean in a separate RUN instruction fail to shrink the image?
- a.Layers are compressed so cleaning is pointless
- b.apt-get clean does not actually remove anything
- c.The cache already persists in the earlier layer, and a later layer cannot delete data from a prior oneCorrect
- d.Cleaning must use yum instead
Why? this is the answer
Each layer is additive; deleting files in a later layer leaves them in the earlier layer's size. Cleaning must happen in the same RUN that created the cache, so option C is correct.
Just read this? Test yourself on what you have been reading.
Read the original → docs.docker.com
- #docker
- #image-size
- #multi-stage
- #optimization
- #best-practices
Put your scrolling time to good use
Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.
The iPhone app is on the way
We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.
Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.
We are hiring for this. Open roles that interview on docker — each one lists the topics its interview covers.
See open roles