Ihr Docker Image ist 5 GB groß? So bekommen Sie es auf 50 MB.
Schritt 1: Messen¶
docker images myapp docker history myapp:latest
Schritt 2: Alpine Base¶
FROM node:20 → 1.1 GB¶
FROM node:20-alpine # → 180 MB
Schritt 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”]
Schritt 4: Distroless¶
FROM gcr.io/distroless/nodejs20-debian12 COPY –from=build /app/dist /app CMD [“/app/index.js”]
Schritt 5: .dockerignore¶
node_modules .git *.md Dockerfile tests
Schritt 6: Layer minimieren¶
RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*
Ergebnisse¶
- node:20 = 1.1 GB
- node:20-alpine = 180 MB
- Multi-stage + Alpine = 80 MB
- Distroless = 50 MB
- Go Static Binary + Scratch = 10 MB
Tipp¶
Kleineres Image = schnelleres Deploy, weniger Angriffsfläche, weniger Speicher. Lohnt sich immer.