How do you reduce Docker image size? Walk through your optimization strategy.

Medium Topic: Docker May 24, 2026

Image size directly affects pull times and attack surface. Key strategies:

  1. Use minimal base images: alpine or distroless instead of ubuntu.
  2. Multi-stage builds: Build in a full image, copy only the binary/artifact to a slim final image.
  3. Combine RUN commands: Each RUN creates a layer. Chain commands with && and clean up in the same layer.
  4. Use .dockerignore: Exclude node_modules, .git, test files from the build context.
# Multi-stage example
FROM node:20 AS builder
WORKDIR /app
COPY . .
RUN npm ci && npm run build

FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
CMD ["node", "dist/index.js"]
← Previous What is the difference between a Docker image... Next → Explain Docker layer caching and how it impacts...

Practice Similar Questions

Back to Docker Topics