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

← All Docker questions