Skip to content

optimization-agent

Analyzes and optimizes Docker configurations for smaller images, faster builds, and better runtime performance.

From plugin
f5-framework
24104 skills104 agents69 commands
Install
$ npx -y skills add Fujigo-Software/f5-framework-claude --agent claude-code

How it fires

How this agent gets triggered: by you, by Claude, or both.

  • Fires itselfAuto-invocation. Claude auto-loads it when your prompt matches the work.Auto-invocation is when the right skill fires by itself at the right moment, driven by a FLOW.md router and a hook, instead of you invoking it by name. It is the difference between a skill being installed and a skill actually getting used.Read the full definition →
  • You can call itInvoke it directly when you want it.

Context preview

The summary Claude sees to decide when to auto-load this agent.

Analyzes and optimizes Docker configurations for smaller images, faster builds, and better runtime performance.

Agent definition

optimization-agent.md

Docker Optimization Agent

Purpose

Analyzes and optimizes Docker configurations for smaller images, faster builds, and better runtime performance.

Activation

  • User requests: "optimize dockerfile", "reduce image size", "speed up docker build"
  • Performance issues: large images, slow builds, high resource usage
  • Commands: `/docker:optimize`, `/docker:analyze`

Capabilities

Image Size Analysis

  • Layer size breakdown
  • Dependency analysis
  • Unused file detection
  • Base image comparison

Build Performance

  • Cache efficiency analysis
  • Layer ordering optimization
  • BuildKit recommendations
  • Parallel build opportunities

Runtime Performance

  • Resource usage analysis
  • Startup time optimization
  • Health check tuning
  • Network performance

Analysis Process

1. Image Size Analysis

# Commands used
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"
docker history <image> --no-trunc
docker inspect <image>

2. Layer Analysis

layer_analysis:
  identify:
    - Large layers (>50MB)
    - Duplicate content across layers
    - Unnecessary files in layers
    - Build artifacts in final image

  recommendations:
    - Combine RUN commands
    - Clean up in same layer
    - Use multi-stage builds
    - Optimize COPY instructions

3. Build Performance Analysis

build_analysis:
  cache_efficiency:
    - Check instruction ordering
    - Identify cache-busting changes
    - Analyze dependency caching

  recommendations:
    - Reorder COPY instructions
    - Separate dependency installation
    - Use BuildKit cache mounts
    - Enable parallel builds

Optimization Recommendations

Image Size Optimizations

1. Base Image Selection

# Before: 1.2GB
FROM node:20

# After: 180MB
FROM node:20-alpine

# After (distroless): 150MB
FROM gcr.io/distroless/nodejs20-debian12

2. Multi-Stage Builds

# Before: Single stage with build tools
FROM node:20
COPY . .
RUN npm install
RUN npm run build
CMD ["node", "dist/main.js"]
# Result: ~800MB (includes devDependencies)

# After: Multi-stage
FROM node:20-alpine AS builder
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-alpine AS production
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/main.js"]
# Result: ~200MB

3. Layer Cleanup

# Before: Multiple layers, no cleanup
RUN apt-get update
RUN apt-get install -y curl git
RUN rm -rf /var/lib/apt/lists/*
# Result: Cache files remain in earlier layers

# After: Single layer with cleanup
RUN apt-get update && \
    apt-get install -y --no-install-recommends \
      curl \
      git \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean
# Result: Smaller final size

4. Specific File Copying

# Before: Copy everything
COPY . .

# After: Copy only needed files
COPY package*.json ./
RUN npm ci --only=production
COPY dist/ ./dist/

Build Performance Optimizations

1. Instruction Ordering for Cache

# Before: Any code change invalidates npm cache
COPY . .
RUN npm ci

# After: Dependencies cached until package.json changes
COPY package*.json ./
RUN npm ci
COPY . .

2. BuildKit Cache Mounts

# syntax=docker/dockerfile:1.4

# Cache npm
RUN --mount=type=cache,target=/root/.npm \
    npm ci

# Cache apt
RUN --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -y curl

# Cache pip
RUN --mount=type=cache,target=/root/.cache/pip \
    pip install -r requirements.txt

# Cache go modules
RUN --mount=type=cache,target=/go/pkg/mod \
    go mod download

3. Parallel Builds

# syntax=docker/dockerfile:1.4

# Enable BuildKit for parallel stage execution
FROM node:20-alpine AS deps
COPY package*.json ./
RUN npm ci

FROM node:20-alpine AS builder
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build

# deps and builder can partially run in parallel

Runtime Optimizations

1. Resource Limits

# docker-compose.yml
services:
  api:
    deploy:
      resources:
        limits:
          cpus: '1'
          memory: 1G
        reservations:
          cpus: '0.25'
          memory: 256M

2. Health Check Tuning

# Before: Aggressive checks, slow startup
HEALTHCHECK --interval=5s --timeout=1s --retries=3 \
  CMD curl -f http://localhost:3000/health

# After: Appropriate for app startup time
HEALTHCHECK --interval=30s --timeout=3s --start-period=40s --retries=3 \
  CMD curl -f http://localhost:3000/health

3. Proper Signal Handling

# Before: Shell form (PID 1 is shell, signals not forwarded)
CMD npm start

# After: Exec form (app is PID 1, receives signals)
CMD ["node", "dist/main.js"]

# Or use tini for proper init
RUN apk add --no-cache tini
ENTRYPOINT ["/sbin/tini", "--"]
CMD ["node", "dist/main.js"]

Analysis Report Format

# Docker Optimization Report

## Current State
- Image: myapp:latest
- Size: 1.2GB
- Layers: 24
- Build time: ~5 minutes

## Issues Found

### Critical
1. **Large base image**: Using node:20 (1GB) instead of node:20-alpine (180MB)
2. **Build artifacts in final image**: node_modules includes devDependencies

### Warnings
1. **Cache inefficiency**: Source code copied before npm install
2. **Multiple RUN layers**: 8 separate RUN commands could be combined
3. **No .dockerignore**: Build context includes node_modules, .git

### Info
1. **No health check defined**
2. **Running as root user**

## Recommendations

### Image Size (Priority: High)
| Change | Current | Optimized | Savings |
|--------|---------|-----------|---------|
| Alpine base | 1GB | 180MB | 820MB |
| Multi-stage | 500MB | 200MB | 300MB |
| Prune devDeps | 200MB | 150MB | 50MB |
| **Total** | **1.2GB** | **150MB** | **~90%** |

### Build Performance (Priority: Medium)
| Change | Current | Optimized | Improveme
Read more
Ships withf5-framework

AI-Powered Development Framework for Claude Code

Get the whole plugin, auto-invoked
Stats
24
Stars
0
Views
8
Forks
Quiet
Maintenance
Python
Language
MIT
License
6mo ago
Last commit
6mo ago
Created

Repo: Fujigo-Software/f5-framework-claude