Generate optimized Dockerfiles using multi-stage builds to reduce production image size and separate build/runtime dependencies
Generates optimized Dockerfiles using Docker's multi-stage build feature to create smaller, more secure production images by separating build-time and runtime dependencies.
This skill helps you create production-ready Dockerfiles that leverage multi-stage builds to:
When the user requests a multi-stage Dockerfile or wants to optimize an existing Dockerfile, follow these steps:
First, understand the project requirements:
Plan the build stages based on the project type:
**For compiled languages (Go, Rust, C++):**
**For interpreted languages (Node.js, Python):**
**For complex applications:**
Create a multi-stage Dockerfile following these best practices:
**Stage naming:**
**Image selection:**
**Layer optimization:**
**Security:**
**Copy between stages:**
Include clear comments in the Dockerfile explaining:
Include example commands for:
Generate or update `.dockerignore` to exclude unnecessary files:
```
node_modules
.git
.env
*.log
dist
coverage
.DS_Store
```
**User request:** "Create a multi-stage Dockerfile for my Express.js API"
**Response:**
```dockerfile
FROM node:20-alpine AS dependencies
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-alpine AS production
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup -g 1001 -S nodejs && \
adduser -S nodejs -u 1001
COPY --from=dependencies --chown=nodejs:nodejs /app/node_modules ./node_modules
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
COPY --chown=nodejs:nodejs package.json ./
USER nodejs
EXPOSE 3000
CMD ["node", "dist/index.js"]
```
**Build command:** `docker build -t express-api:latest .`
**User request:** "Optimize my Go application Dockerfile"
**Response:**
```dockerfile
FROM golang:1.21-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main .
FROM alpine:3.19
RUN apk --no-cache add ca-certificates
WORKDIR /root/
COPY --from=builder /app/main .
EXPOSE 8080
CMD ["./main"]
```
**Size comparison:** Builder stage ~500MB, final image ~15MB
**User request:** "Create a Dockerfile that runs tests in a separate stage"
**Response:**
```dockerfile
FROM python:3.11-slim AS base
WORKDIR /app
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt
FROM base AS test
COPY requirements-dev.txt .
RUN pip install --no-cache-dir -r requirements-dev.txt
COPY . .
RUN pytest tests/
FROM python:3.11-slim AS production
WORKDIR /app
RUN useradd -m -u 1000 appuser
COPY --from=base /usr/local/lib/python3.11/site-packages /usr/local/lib/python3.11/site-packages
COPY --from=base /usr/local/bin /usr/local/bin
COPY --chown=appuser:appuser . .
USER appuser
CMD ["python", "app.py"]
```
**Build production:** `docker build --target production -t app:prod .`
**Run tests:** `docker build --target test -t app:test .`
When appropriate, suggest:
Leave a review
No reviews yet. Be the first to review this skill!
# Download SKILL.md from killerskills.ai/api/skills/docker-multi-stage-build-generator/raw