optimize-docker-build-cache
关于
This Claude Skill optimizes Docker builds by implementing layer caching, multi-stage builds, and BuildKit features to drastically reduce build times. It's designed for R, Node.js, and Python projects where repeated dependency installations or large image sizes are a bottleneck. Use it when slow rebuilds or CI/CD pipeline delays are caused by inefficient Dockerfile structure.
快速安装
Claude Code
推荐npx skills add pjt222/agent-almanac -a claude-code/plugin add https://github.com/pjt222/agent-almanacgit clone https://github.com/pjt222/agent-almanac.git ~/.claude/skills/optimize-docker-build-cache在 Claude Code 中复制并粘贴此命令以安装该技能
技能文档
Optimize Docker Build Cache
Cut build times via layer cache + opt.
Use When
- Builds slow → repeated pkg installs
- Rebuilds reinstall all deps on code change
- Images too big
- CI/CD bottleneck
In
- Required: Existing Dockerfile to optimize
- Optional: Target build time
- Optional: Target image size reduction
Do
Step 1: Order layers by change freq
Least-changing first.
# 1. Base image (rarely changes)
FROM rocker/r-ver:4.5.0
# 2. System dependencies (change occasionally)
RUN apt-get update && apt-get install -y \
libcurl4-openssl-dev \
libssl-dev \
&& rm -rf /var/lib/apt/lists/*
# 3. Dependency files only (change when deps change)
COPY renv.lock renv.lock
COPY renv/activate.R renv/activate.R
RUN R -e "renv::restore()"
# 4. Source code (changes frequently)
COPY . .
Key: Docker caches each layer. Layer changes → all subsequent rebuild. Deps install BEFORE source copy.
→ Layers ordered least-changing → most-changing, lockfiles before full source.
If err: still reinstalls on code change → verify COPY . . AFTER RUN deps install, not before.
Step 2: Separate deps from code
Bad (rebuild pkgs every code change):
COPY . .
RUN R -e "renv::restore()"
Good (rebuild only on lockfile change):
COPY renv.lock renv.lock
RUN R -e "renv::restore()"
COPY . .
Same for Node.js:
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
→ Lockfile (renv.lock, package-lock.json, requirements.txt) copy + install in separate layer before full COPY . ..
If err: lockfile copy fails → verify file exists in build context, not excluded by .dockerignore.
Step 3: Multi-stage builds
Split build vs runtime.
# Build stage - includes dev tools
FROM rocker/r-ver:4.5.0 AS builder
RUN apt-get update && apt-get install -y \
libcurl4-openssl-dev libssl-dev build-essential
COPY renv.lock .
RUN R -e "install.packages('renv'); renv::restore()"
# Runtime stage - minimal image
FROM rocker/r-ver:4.5.0
RUN apt-get update && apt-get install -y \
libcurl4 libssl3 \
&& rm -rf /var/lib/apt/lists/*
COPY --from=builder /usr/local/lib/R/site-library /usr/local/lib/R/site-library
COPY . /app
WORKDIR /app
CMD ["Rscript", "main.R"]
→ Builder stage (dev tools) + runtime (prod only). Final image much smaller than single-stage.
If err: COPY --from=builder can't find libs → verify install paths match. Debug w/ docker build --target builder ..
Step 4: Combine RUN commands
Each RUN = layer. Combine related.
Bad (3 layers, apt cache persists):
RUN apt-get update
RUN apt-get install -y curl git
RUN rm -rf /var/lib/apt/lists/*
Good (1 layer, clean cache):
RUN apt-get update && apt-get install -y \
curl \
git \
&& rm -rf /var/lib/apt/lists/*
→ Related apt-get / pkg installs combined into single RUN, each ending w/ cleanup (rm -rf /var/lib/apt/lists/*).
If err: combined RUN fails midway → split temporarily to ID failing cmd, recombine after fix.
Step 5: .dockerignore
Block unnecessary files from build context.
.git
.Rproj.user
.Rhistory
.RData
renv/library
renv/cache
node_modules
docs/
*.tar.gz
.env
→ .dockerignore in root excludes .git, node_modules, renv/library, build artifacts, env files. Build context noticeably smaller.
If err: needed files missing in container → check .dockerignore for too-broad patterns. Verbose docker build output to verify what's sent.
Step 6: BuildKit
DOCKER_BUILDKIT=1 docker build -t myimage .
Or docker-compose.yml:
services:
app:
build:
context: .
dockerfile: Dockerfile
W/ COMPOSE_DOCKER_CLI_BUILD=1 + DOCKER_BUILDKIT=1 env vars.
BuildKit gives:
- Parallel stage builds
- Better cache mgmt
--mount=type=cachefor persistent pkg caches
→ BuildKit active (#1 [internal] load build definition style output). Multi-stage parallel where possible.
If err: BuildKit inactive → verify env vars exported pre-build. Old Docker → upgrade Engine 18.09+.
Step 7: Cache mounts for pkg mgrs
# R packages with persistent cache
RUN --mount=type=cache,target=/usr/local/lib/R/site-library \
R -e "install.packages('dplyr')"
# npm with persistent cache
RUN --mount=type=cache,target=/root/.npm \
npm ci
→ Subsequent builds reuse cached pkgs from mount → dramatic install time cut even when layer invalidated. Cache persists across builds.
If err: --mount=type=cache not recognized → BuildKit needed (DOCKER_BUILDKIT=1). Legacy builder doesn't support.
Check
- Code-only rebuilds significantly faster
- Deps layer cached when lockfile unchanged
-
.dockerignoreexcludes unnecessary - Image size reduced
- Multi-stage (if used) splits build/runtime
Traps
- Copy all before install: invalidates cache every code change
- No
.dockerignore: big context → every build slow - Too many layers: each
RUN/COPY/ADD= layer. Combine logically - No apt cache clean: always end w/
&& rm -rf /var/lib/apt/lists/* - Platform-specific caches: layers platform-specific. CI runners may not benefit from local
→
create-r-dockerfile— initial Dockerfilesetup-docker-compose— compose build configcontainerize-mcp-server— apply opts to MCP servers
GitHub 仓库
相关推荐技能
content-collections
元Content Collections 是一个 TypeScript 优先的构建工具,可将本地 Markdown/MDX 文件转换为类型安全的数据集合。它专为构建博客、文档站和内容密集型 Vite+React 应用而设计,提供基于 Zod 的自动模式验证。该工具涵盖从 Vite 插件配置、MDX 编译到生产环境部署的完整工作流。
polymarket
元这个Claude Skill为开发者提供完整的Polymarket预测市场开发支持,涵盖API调用、交易执行和市场数据分析。关键特性包括实时WebSocket数据流,可监控实时交易、订单和市场动态。开发者可用它构建预测市场应用、实施交易策略并集成实时市场预测功能。
creating-opencode-plugins
元该Skill帮助开发者创建OpenCode插件,用于接入命令、文件、LSP等25+种事件。它提供了插件结构、事件API规范和JavaScript/TypeScript实现模式,适合需要拦截操作、扩展功能或自定义事件处理的场景。开发者可通过它快速构建响应式模块来增强OpenCode AI助手的能力。
sglang
元SGLang是一个专为LLM设计的高性能推理框架,特别适用于需要结构化输出的场景。它通过RadixAttention前缀缓存技术,在处理JSON、正则表达式、工具调用等具有重复前缀的复杂工作流时,能实现极速生成。如果你正在构建智能体或多轮对话系统,并追求远超vLLM的推理性能,SGLang是理想选择。
