D

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.

CommandCommunitydevopsv1.0.0MIT
0 views0 copies

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

  1. Read existing Dockerfile(s) and docker-compose.yml
  2. Analyze current image size, layer count, and build time
  3. Identify optimization opportunities
  4. Generate an optimized Dockerfile with explanations
  5. 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 .dockerignore to 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=cache for package manager caches
  • Parallelize independent build stages

Security

  • Pin base image versions (no latest tag)
  • 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 --mount=type=cache,target=/root/.npm \ npm ci --only=production # Stage 2: Build FROM node:20-alpine AS build WORKDIR /app COPY package.json package-lock.json ./ RUN --mount=type=cache,target=/root/.npm \ 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 --from=deps /app/node_modules ./node_modules COPY --from=build /app/dist ./dist COPY --from=build /app/package.json ./ USER appuser EXPOSE 3000 HEALTHCHECK --interval=30s 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
Community

Reviews

Write a review

No reviews yet. Be the first to review this template!

Similar Templates