Optimize Docker builds with multi-stage builds
✓Works with OpenClaudeYou are a Docker optimization expert. The user wants to implement multi-stage builds to reduce final image size and optimize Docker build performance.
What to check first
- Run
docker --versionto confirm Docker is installed (v17.05+) - Check your current Dockerfile with
cat Dockerfileto identify build artifacts that could be removed - Run
docker imagesto see current image sizes before optimization
Steps
- Create a new
Dockerfilewith multipleFROMstatements—eachFROMstarts a new build stage - In the first stage (builder), install build tools and compile/transpile your application
- Use
COPY --from=<stage>to copy only compiled artifacts from the builder stage to the final stage - In the final stage, use a minimal base image (alpine, distroless, or scratch) with only runtime dependencies
- Add labels and metadata in the final stage with
LABELinstructions - Build with
docker build -t myapp:latest .and inspect the resulting image size - Compare file sizes using
docker history myapp:latestto verify build artifacts were stripped - Push to registry or run with
docker run myapp:latest
Code
# Stage 1: Builder
FROM node:18-alpine AS builder
WORKDIR /build
# Install build dependencies
RUN apk add --no-cache python3 make g++
# Copy source and package files
COPY package*.json ./
RUN npm ci --only=production && npm run build
# Stage 2: Runtime
FROM node:18-alpine
WORKDIR /app
# Add security labels
LABEL maintainer="your-email@example.com"
LABEL description="Optimized Node.js app with multi-stage build"
# Copy runtime dependencies and built artifacts from builder
COPY --from=builder /build/node_modules ./node_modules
COPY --from=builder /build/dist ./dist
COPY --from=builder /build/package.json .
# Non-root user for security
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
USER nodejs
# Health check
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD node -e "require('http').get('http://localhost:3000/health', (r) => {if (r.statusCode !== 200) throw new Error(r.statusCode)})"
EXPOSE 3000
CMD ["node", "dist/index.js"]
Pitfalls
- Forgetting to exclude node_modules from builder stage: Use
.dockerignorewithnode_modulesanddistto prevent copying unnecessary files into the builder layer - Using outdated base images: Alpine is lightweight but may have glibc compatibility issues; use
node:18-alpinefor Node or distroless images for production - Not specifying exact stage name in COPY --from: If you reference a stage, use the exact name after
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Docker & Kubernetes Skills
Other Claude Code skills in the same category — free to download.
Dockerfile Generator
Generate optimized Dockerfile for any project
Docker Compose
Create docker-compose.yml for multi-service apps
K8s Deployment
Generate Kubernetes deployment manifests
K8s Service
Create Kubernetes service and ingress configs
Helm Chart
Create Helm chart for application
K8s ConfigMap
Create ConfigMaps and Secrets management
Docker Security
Audit and fix Dockerfile security issues
K8s HPA
Set up Horizontal Pod Autoscaler
Want a Docker & Kubernetes skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.