optimize-docker-layers
安装方式
先用 Prompt 核对来源与依赖。也可下载安装包。
用 AI 助手安装
复制提示词,让助手先核对固定版本和依赖。
查看 Prompt收起 Prompt
先检查当前 Agent 是否已安装 SkillPI Skill Finder。如果尚未安装,请按官方仓库的说明安装并启用:https://github.com/Findskills/skillpi。然后使用 Skill Finder 打开此 SkillPI 详情页并定位 "optimize-docker-layers":https://skillpi.com/zh/skills/deepskill-504a83bc64aecf5363be39a93de5cad591519e99e79389ae1ad5485a71e94d7d。安装前核对上游来源和版本:https://github.com/dykyi-roman/awesome-claude-code。来源不一致时停止并说明差异。核对通过后,用 Skill Finder 安装目标 Skill,并报告安装路径和来源版本。
来源与配置
github.com
配置方式待核实
请查看原始来源中的适用工具、依赖和权限要求。
安装实测待补充
分析并优化 PHP 项目的 Docker 层缓存。识别层顺序问题和导致缓存失效的配置,给出可操作的建议以加快镜像构建速度。
介绍来源: third_party · dykyi-roman · 0.0.1 参考来源
SkillPi 解读
- 能完成的任务
- 翻译待完成,请参阅来源文档。
- 输入和输出
- 减记
- 实测状态
- 未测试
文件浏览
Frontmatter
name: optimize-docker-layers
description: Analyzes and optimizes Docker layer caching for PHP projects. Identifies layer ordering issues, cache invalidation problems, and provides recommendations for faster builds.Docker Layer Optimization
Analyzes Dockerfiles and provides optimization recommendations for faster CI builds.
Layer Caching Principles
┌─────────────────────────────────────────────────────────────────┐
│ DOCKER LAYER CACHE │
├─────────────────────────────────────────────────────────────────┤
│ │
│ Layer 1: FROM php:8.4-alpine ✓ Cached (base image) │
│ ↓ │
│ Layer 2: RUN apk add ... ✓ Cached (system deps) │
│ ↓ │
│ Layer 3: COPY composer.json ... ✓ Cached (if unchanged) │
│ ↓ │
│ Layer 4: RUN composer install ✓ Cached (if lock same) │
│ ↓ │
│ Layer 5: COPY . . ✗ INVALIDATED (source) │
│ ↓ │
│ Layer 6: RUN build commands ✗ Must rebuild │
│ │
│ Rule: When a layer changes, all subsequent layers rebuild │
└─────────────────────────────────────────────────────────────────┘
Anti-Patterns and Fixes
1. Copying All Files Too Early
# ❌ BAD: Invalidates cache on ANY file change
FROM php:8.4-cli
COPY . /app
RUN composer install
# ✅ GOOD: Only invalidates on composer changes
FROM php:8.4-cli
COPY composer.json composer.lock /app/
RUN composer install
COPY . /app
2. Installing Dev Dependencies in Production
# ❌ BAD: Includes dev dependencies in production
FROM php:8.4-fpm
COPY . /app
RUN composer install
# ✅ GOOD: Multi-stage with production deps
FROM composer:2 AS deps
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist
FROM php:8.4-fpm
COPY --from=deps /app/vendor /app/vendor
COPY . /app
3. Combining Unrelated Commands
# ❌ BAD: One change invalidates entire layer
RUN apt-get update && \
apt-get install -y git && \
composer install && \
npm install && \
npm run build
# ✅ GOOD: Separate concerns into layers
RUN apt-get update && apt-get install -y git
COPY composer.json composer.lock ./
RUN composer install
COPY package.json package-lock.json ./
RUN npm ci && npm run build
4. Not Using .dockerignore
# ❌ BAD: Copies unnecessary files
COPY . .
# Includes: vendor, node_modules, .git, tests, etc.
# ✅ GOOD: .dockerignore
.git
vendor
node_modules
tests
docs
*.md
5. Running apt-get update Separately
# ❌ BAD: Stale package cache
RUN apt-get update
RUN apt-get install -y git curl
# ✅ GOOD: Combined update and install
RUN apt-get update && apt-get install -y \
git \
curl \
&& rm -rf /var/lib/apt/lists/*
Optimization Checklist
Layer Ordering
| Order | Content | Frequency of Change |
|---|---|---|
| 1 | Base image | Rarely |
| 2 | System packages | Monthly |
| 3 | PHP extensions | Monthly |
| 4 | Composer dependencies | Weekly |
| 5 | NPM dependencies | Weekly |
| 6 | Application code | Every commit |
| 7 | Build artifacts | Every commit |
Cache Optimization
# Optimal layer ordering example
FROM php:8.4-fpm-alpine
# Layer 1-2: System dependencies (changes rarely)
RUN apk add --no-cache libzip icu-libs
# Layer 3: PHP extensions (changes monthly)
RUN docker-php-ext-install pdo_mysql intl zip
# Layer 4: Composer deps (changes weekly)
COPY composer.json composer.lock ./
RUN composer install --no-dev --prefer-dist
# Layer 5: NPM deps if needed (changes weekly)
COPY package*.json ./
RUN npm ci --production
# Layer 6: Source code (changes every commit)
COPY . .
# Layer 7: Build step (depends on source)
RUN composer dump-autoload --optimize
BuildKit Cache Mounts
Composer Cache Mount
# syntax=docker/dockerfile:1.6
FROM php:8.4-cli
# Cache composer packages between builds
RUN --mount=type=cache,target=/root/.composer/cache \
composer install --prefer-dist
APK Cache Mount
# syntax=docker/dockerfile:1.6
FROM php:8.4-alpine
# Cache apk packages
RUN --mount=type=cache,target=/var/cache/apk \
apk add --cache-dir=/var/cache/apk git unzip
NPM Cache Mount
# syntax=docker/dockerfile:1.6
FROM node:20-alpine
RUN --mount=type=cache,target=/root/.npm \
npm ci
Multi-Stage Build Patterns
Minimal Production Image
# Stage 1: Build with all tools
FROM php:8.4-cli AS builder
RUN apt-get update && apt-get install -y git unzip
COPY --from=composer:2 /usr/bin/composer /usr/bin/composer
COPY . .
RUN composer install --no-dev --optimize-autoloader
# Stage 2: Production with only runtime
FROM php:8.4-fpm-alpine AS production
COPY --from=builder /app/vendor /app/vendor
COPY --from=builder /app/src /app/src
COPY --from=builder /app/public /app/public
Parallel Builds
# syntax=docker/dockerfile:1.6
# Build PHP deps in parallel with JS deps
FROM composer:2 AS php-deps
COPY composer.* ./
RUN composer install --no-dev
FROM node:20 AS js-deps
COPY package*.json ./
RUN npm ci
FROM php:8.4-fpm
COPY --from=php-deps /app/vendor ./vendor
COPY --from=js-deps /app/node_modules ./node_modules
CI Platform Caching
GitHub Actions
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Build with cache
uses: docker/build-push-action@v5
with:
context: .
cache-from: type=gha
cache-to: type=gha,mode=max
GitLab CI
build:
script:
- docker build
--cache-from $CI_REGISTRY_IMAGE:latest
--tag $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
.
- docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
Docker Layer Cache with Registry
# Pull previous image for cache
- docker pull $REGISTRY/app:latest || true
# Build with cache from pulled image
- docker build
--cache-from $REGISTRY/app:latest
--tag $REGISTRY/app:$VERSION
.
Image Size Optimization
Alpine vs Debian
# Debian: ~150MB base
FROM php:8.4-fpm
# Final: ~400-500MB
# Alpine: ~50MB base
FROM php:8.4-fpm-alpine
# Final: ~100-200MB
Remove Build Dependencies
FROM php:8.4-fpm-alpine
# Install and clean in one layer
RUN apk add --no-cache --virtual .build-deps \
$PHPIZE_DEPS \
libzip-dev \
&& docker-php-ext-install zip \
&& apk del .build-deps \
&& apk add --no-cache libzip
Use Specific Tags
# ❌ BAD: Unpredictable
FROM php:latest
# ✅ GOOD: Deterministic
FROM php:8.4.2-fpm-alpine3.19
Analysis Output Format
## Docker Layer Analysis
### Image: app:latest
**Size:** 450MB
**Layers:** 12
### Issues Found
| Severity | Issue | Location | Impact |
|----------|-------|----------|--------|
| 🔴 High | COPY before deps | Line 5 | Cache invalidation |
| 🟠 Medium | No .dockerignore | - | 50MB+ extra |
| 🟡 Low | Combined commands | Line 12 | Poor caching |
### Recommendations
1. **Move COPY . . after dependency install**
```dockerfile
# Before
COPY . .
RUN composer install
# After
COPY composer.* ./
RUN composer install
COPY . .
Impact: -2-5 minutes per build
- Add .dockerignore
.git vendor node_modulesImpact: -50MB image size
Estimated Improvements
| Metric | Before | After | Improvement |
|---|---|---|---|
| Build time | 8m | 3m | -62% |
| Image size | 450MB | 180MB | -60% |
| Cache hit rate | 20% | 80% | +60% |
## Generation Instructions
1. **Analyze Dockerfile:**
- Parse layer order
- Identify COPY commands
- Check RUN command grouping
- Verify .dockerignore exists
2. **Check for anti-patterns:**
- Early COPY of all files
- Combined unrelated commands
- Missing cache mounts
- No multi-stage build
3. **Generate recommendations:**
- Reorder layers
- Split/combine commands
- Add cache mounts
- Optimize for CI platform
## Usage
Provide:
- Path to Dockerfile
- CI platform (GitHub Actions, GitLab CI)
- Current build time (optional)
The analyzer will:
1. Parse Dockerfile layers
2. Identify optimization opportunities
3. Calculate potential improvements
4. Generate optimized Dockerfile当前收录版本
0.0.1
质量评测
满分100 · 来自 SkillPi LAB 实验室的评测报告。
亮点
- 直接解决 CI/CD 中 Docker 构建速度的核心痛点
- 提供详细的层缓存优化策略和最佳实践
- 包含多平台 CI 配置示例,实用性强
亮点
- 触发词精准完整,覆盖多种变体
- 边界清晰,不易与其他 Skill 重叠
亮点
- Phase/步骤清晰,有出口条件
- 含端到端示例,frontmatter完整
- 单文件≤500行,结构良好
亮点
- 工具来源明确,参数正确
- 覆盖主要异常,不可逆操作有确认机制
取前四项原始分值的算术平均,四舍五入取整。可通过收录报告核对各项评分与说明,运行记录需单独查证。
正文静态检测
100 / 100 · skillpi-20261123-no-r3-v1
正文未命中当前规则。
分数仅反映正文中的规则命中,供使用前参考。附件与运行行为不在此评分范围内。
权限与使用前检查
根据当前主文档自动整理,命中内容可能是示例或防护说明;不是实际权限声明,也未覆盖附件、外部依赖或运行行为。
-
删除文件
先确认绝对路径及作用范围,保留可恢复备份;不要直接对重要目录运行示例命令。
主文档提及 · 第 125 行 -
安装或获取外部依赖
使用官方来源并锁定版本,在隔离环境检查安装脚本。当前文档检查不覆盖后续下载的代码。
主文档提及 · 第 77, 79, 85, 117, 122 行
安全评级与访问范围
safe · 静态证据核验,不代表运行安全保证。
使用前请由宿主核对并授权实际文件读写、外部服务和联网目的地;本站的核验不会授予任何运行权限。
用户讨论
还没有已核实的点评。