Docker interview question · Most asked
How do you make Docker images smaller?
Summary
Start from a minimal base (slim, Alpine, distroless or scratch for static binaries), use multi-stage builds to leave build tools behind, install only runtime dependencies (--no-install-recommends, production-only packages), clean caches in the same layer, exclude unneeded files with .dockerignore, and inspect layers to find what is large.
Longer Answer
Tools: docker image history and dive show what each layer adds. Alpine uses musl libc, which can cause subtle differences or slow builds for languages with native extensions (Python wheels often target glibc), so -slim Debian-based images are a safer default for many stacks; distroless images contain only the runtime and no shell, which also reduces attack surface (debug with ephemeral debug containers).
Smaller images pull faster, which directly speeds up autoscaling and deploys, and have fewer packages to patch.
Key Points
- Minimal bases; multi-stage.
- Runtime dependencies only; same-layer cleanup.
- dive / history to inspect.
- Alpine musl caveats; distroless has no shell.
More most-asked Docker questions
- What is the difference between an image and a container, and how do image layers work?
- What is a container, and how does it differ from a virtual machine?
- What are multi-stage builds, and why use them?
- CMD or ENTRYPOINT: what is the difference, and what are exec and shell forms?
- How does Docker build caching work, and how do you order a Dockerfile to use it well?
- What are Dockerfile best practices for production images?
- ARG or ENV: what is the difference, and how do you pass secrets into a build safely?
- How do you secure Docker containers and images?