AgentSkillsCN

optimizing

分析Docker镜像的各层结构,提出优化建议,以缩减镜像体积、提升构建速度并增强缓存效率。

SKILL.md
--- frontmatter
name: optimizing
description: Analyze Docker image layers and suggest optimizations for size reduction, build speed, and caching efficiency.
allowed-tools:
  - Read
  - Write
  - Glob
  - Grep
  - AskUserQuestion
  - Bash(docker images *)
  - Bash(docker inspect *)
  - Bash(docker history *)

Optimize Docker Images

Analyze Docker image layers and apply optimizations for size and build performance.

Analysis Steps

1. Check Current Image

bash
docker images {image_name} --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}"

2. Analyze Layers

bash
docker history {image_name}:{tag} --no-trunc --format "table {{.Size}}\t{{.CreatedBy}}"

3. Read Dockerfile

text
Glob: Dockerfile, Dockerfile.*

Read and analyze the current Dockerfile structure.

Optimization Checks

Size Optimizations

CheckOptimization
Base imageSwitch to alpine/slim/distroless
Multi-stageConvert single-stage to multi-stage
Dev deps in runtimeRemove build tools from final stage
Package cacheClean apt/apk cache in same RUN layer
Unnecessary filesAdd to .dockerignore
Layer countCombine related RUN commands

Build Speed Optimizations

CheckOptimization
Layer orderingMove frequently changing layers last
Cache mountsAdd --mount=type=cache for package managers
Dependency cachingCopy lock files before source code
BuildKit featuresEnable DOCKER_BUILDKIT=1

Caching Optimizations

CheckOptimization
Lock file firstCOPY package.json before COPY .
Source lastApplication code copied last
Build cacheUse --mount=type=cache

Workflow

1. Analyze Current State

Gather image size, layer count, and Dockerfile content.

2. Identify Optimizations

Compare against checklist and identify applicable improvements.

3. Present Findings

Show before/after comparison for each optimization with estimated impact.

4. Ask User

Via AskUserQuestion:

  • "Apply all optimizations" - Rewrite Dockerfile
  • "Apply selected" - Choose which to apply
  • "Report only" - No changes

5. Apply Changes

If requested, rewrite Dockerfile with optimizations applied.

6. Generate Report

Use optimization-report.md template.

Common Optimizations

Multi-stage Conversion

Before:

dockerfile
FROM python:3.12
COPY . .
RUN pip install -r requirements.txt
CMD ["python", "main.py"]

After:

dockerfile
FROM python:3.12-slim AS builder
COPY requirements.txt .
RUN pip install --no-cache-dir -r requirements.txt

FROM python:3.12-slim
COPY --from=builder /usr/local/lib/python3.12/site-packages /usr/local/lib/python3.12/site-packages
COPY . .
CMD ["python", "main.py"]

Cache Mount Addition

Before:

dockerfile
RUN pip install -r requirements.txt

After:

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

Base Image Switch

FromToSavings
python:3.12python:3.12-slim~700MB
node:20node:20-slim~600MB
ubuntu:22.04debian:bookworm-slim~50MB