How do I reduce Docker image size?

Updated October 2026 · How we answer

Short answerUse smaller base images (like Alpine), combine RUN commands, remove unnecessary files, and use multi-stage builds to keep only what's needed in the final image.

Choose a Smaller Base Image

The base image often dominates image size. Official images like 'node:18' are based on Debian and can be 300–900 MB. Switching to 'node:18-alpine' (Alpine Linux) cuts that to around 50–150 MB. Even smaller options include 'distroless' images or scratch for static binaries.

However, Alpine uses musl libc instead of glibc, which can cause compatibility issues with some native modules. Test your app before switching. For Python, 'python:3.11-slim' is a good middle ground.

  • alpine: ~5 MB base
  • debian-slim: ~30 MB base
  • distroless: minimal, no shell
  • scratch: empty, for static binaries

Optimize Layers and Clean Up

Each RUN instruction creates a layer that adds to the final size. Combine commands with && and clean up package manager caches in the same RUN. For example: 'RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*'. This prevents cache files from being stored in a layer.

Use a .dockerignore file to exclude node_modules, .git, and other unnecessary files from the build context. This speeds up builds and prevents accidental inclusion of large files.

Use Multi-Stage Builds

Multi-stage builds let you use one image for building and another for running. You copy only the compiled artifacts into the final stage. For example, build a Go binary in a 'golang' image, then copy it into a 'scratch' or 'alpine' image. This can reduce image size from hundreds of MB to a few MB.

In your Dockerfile, define multiple FROM statements and use 'COPY --from=builder /app/binary /binary'. The final image contains only the binary and its runtime dependencies, not the build tools.

Common mistakes

  • Running 'apt-get update' and 'apt-get install' in separate RUN instructions, causing stale cache and larger layers.
  • Forgetting to clean up temporary files or package caches in the same layer where they were created.
  • Using 'latest' tags for base images, which can lead to unexpected size increases when the tag updates.
From our shopsTitan Case: Premium MagSafe iPhone cases with a precision fit.