Your Docker image is 5 GB? Here is how to get it down to 50 MB.
Step 1: Measure¶
docker images myapp docker history myapp:latest
Step 2: Alpine Base¶
Docker Image: From 5 GB to 50 MB¶
FROM node:20-alpine # → 180 MB
Step 3: Multi-stage Build¶
FROM node:20-alpine AS build WORKDIR /app COPY package*.json ./ RUN npm ci –production COPY . . RUN npm run build
FROM node:20-alpine COPY –from=build /app/dist ./dist COPY –from=build /app/node_modules ./node_modules CMD [“node”, “dist/index.js”]
Step 4: Distroless¶
FROM gcr.io/distroless/nodejs20-debian12 COPY –from=build /app/dist /app CMD [“/app/index.js”]
Step 5: .dockerignore¶
node_modules .git *.md Dockerfile tests
Step 6: Minimize Layers¶
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
Results¶
- node:20 = 1.1 GB
- node:20-alpine = 180 MB
- Multi-stage + alpine = 80 MB
- Distroless = 50 MB
- Go static binary + scratch = 10 MB
Tip¶
Smaller image = faster deploy, less attack surface, less storage. Always worth it.