| Ko

The Secrets to Slash Docker Image Size by 10x

Before Optimization A typical Dockerfile for a Node.js application looks like this: FROM node:18 WORKDIR /app COPY . . RUN npm install CMD ["npm", "start"] This image ends up being over 1GB in size. The main reasons for this are: Heavy base image Development tools included Presence of unnecessary files Accumulation of cache files Optimization Techniques 1. Implement Multi-Stage Builds Separate the build and runtime stages. # Build stage FROM node:18-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm install COPY . . RUN npm run build # Runtime stage FROM node:18-alpine WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/package*.json ./ RUN npm install --production CMD ["npm", "start"] 2. Use Alpine Linux Alpine Linux significantly reduces the base image size. ...

February 17, 2025 · 3 min · 464 words · In-Jun
[email protected]