Reducing Docker Image Size: Step-by-Step Checklist
Thu Aug 14 2025
Bloated images slow CI/CD and waste bandwidth. Optimize with intent.
1. Start with a Baseline
docker build -t app:baseline . docker image inspect app:baseline --format='{{.Size}}'
Record MB size.
2. Choose a Smaller Base
Prefer:
node:20-alpine
overnode:20
python:3.12-slim
overpython:3.12
3. Multi-Stage Builds
FROM node:20-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci --omit=dev COPY . . RUN npm run build FROM node:20-alpine AS runtime WORKDIR /app COPY --from=build /app/.next ./.next COPY --from=build /app/node_modules ./node_modules COPY package.json . CMD ["node", "server.js"]
4. Prune Dev Dependencies
Use npm ci --omit=dev
(or pnpm install --prod
).
5. Collapse Layers
Combine adjacent RUNs:
RUN apk add --no-cache libc6-compat && \ adduser -D app && \ chown -R app:app /app
6. Remove Build Artifacts
- Delete unused docs/tests.
- Strip source maps in final image if not needed.
7. Use .dockerignore
Add:
.git node_modules Dockerfile* *.log coverage
8. Scan for Bloat
dive app:baseline
Look for large layers or vendored blobs.
9. Add Health & User Hardening
USER app HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1
10. Measure Again
Rebuild and compare size delta. Target >30% reduction.
Ship leaner, ship faster.