Docker Optimize Command
Analyze and optimize Dockerfiles for smaller images, faster builds, and production security. Implements multi-stage builds, layer caching strategies, security hardening, and image size reduction techniques.
Command
/docker-optimize
Description
Analyzes your Dockerfile(s) and docker-compose configuration to identify optimization opportunities. Produces a rewritten Dockerfile with multi-stage builds, optimized layer ordering, security hardening, and size reduction.
Behavior
- Read existing Dockerfile(s) and docker-compose.yml
- Analyze current image size, layer count, and build time
- Identify optimization opportunities
- Generate an optimized Dockerfile with explanations
- Compare before/after metrics
Optimization Checklist
Image Size Reduction
- Use slim/alpine base images where possible
- Multi-stage builds to separate build and runtime
- Remove build dependencies from final image
- Clean package manager caches (
apt-get clean,npm cache clean) - Use
.dockerignoreto exclude unnecessary files - Minimize layer count by combining RUN commands
Build Speed
- Order layers from least to most frequently changed
- Copy dependency manifests before source code
- Leverage build cache effectively
- Use
--mount=type=cachefor package manager caches - Parallelize independent build stages
Security
- Pin base image versions (no
latesttag) - Run as non-root user
- No secrets in build args or layers
- Scan for known vulnerabilities
- Minimal runtime packages (no curl, wget in production)
- Read-only filesystem where possible
Output Format
Optimized Dockerfile Example (Node.js)
# Stage 1: Dependencies FROM node:20-alpine AS deps WORKDIR /app COPY package.json package-lock.json ./ RUN \ npm ci --only=production # Stage 2: Build FROM node:20-alpine AS build WORKDIR /app COPY package.json package-lock.json ./ RUN \ npm ci COPY . . RUN npm run build # Stage 3: Runtime FROM node:20-alpine AS runtime WORKDIR /app # Security: non-root user RUN addgroup -g 1001 appgroup && \ adduser -u 1001 -G appgroup -s /bin/sh -D appuser # Copy only production artifacts COPY /app/node_modules ./node_modules COPY /app/dist ./dist COPY /app/package.json ./ USER appuser EXPOSE 3000 HEALTHCHECK CMD wget -qO- http://localhost:3000/health || exit 1 CMD ["node", "dist/index.js"]
Comparison Report
## Optimization Results | Metric | Before | After | Improvement | |--------|--------|-------|-------------| | Image Size | 1.2 GB | 185 MB | -85% | | Layers | 23 | 12 | -48% | | Build Time | 4m 30s | 1m 45s | -61% | | CVEs | 12 | 0 | -100% | | Runs as Root | Yes | No | Fixed |
Examples
# Optimize the project Dockerfile /docker-optimize # Optimize with specific base image preference /docker-optimize --base alpine # Include docker-compose optimization /docker-optimize --include-compose
Reviews
No reviews yet. Be the first to review this template!
Similar Templates
Git Commit Message Generator
Generates well-structured conventional commit messages by analyzing staged changes. Follows Conventional Commits spec with scope detection.
React Component Scaffolder
Scaffolds a complete React component with TypeScript types, Tailwind styles, Storybook stories, and unit tests. Follows project conventions automatically.
CI/CD Pipeline Generator
Generates GitHub Actions workflows for CI/CD including linting, testing, building, and deploying. Detects project stack automatically.