做什么
写出优化、安全、多阶段具有语言特有模式的多克文件(Python、Node.js、Go、Rust), 构建 Kit 特性, 和不动的图像
技能库 智客分类:运维与云 writing-dockerfiles
写出优化、安全、多阶段具有语言特有模式的多克文件(Python、Node.js、Go、Rust), 构建 Kit的特征 和无畏的图像 在容器化应用时使用,优化已存在的多克文件,或缩小图像大小.
官方网址:skills.sh
先看中文介绍;官方 description 原文单独保留,不改写 SKILL.md。
写出优化、安全、多阶段具有语言特有模式的多克文件(Python、Node.js、Go、Rust), 构建 Kit 特性, 和不动的图像
这个技能
按 Agent Skills 渐进披露:启动时只加载 name 与 description(约 100 token);任务匹配后才读入整份 SKILL.md 正文;scripts/、references/、assets/ 仅在需要时再读。 本文件正文结构:Writing Dockerfiles、When to Use This Skill、Quick Decision Framework、Core Concepts、Multi-Stage Builds、Stage 1: Build。
文件分析:除 SKILL.md 外,正文引用了 references/python-dockerfiles.md、references/nodejs-dockerfiles.md、references/go-dockerfiles.md、references/rust-dockerfiles.md、references/java-dockerfiles.md、references/security-hardening.md,属于带资源的技能包,这些文件按需再读。
Writing optimized, secure, multi-stage Dockerfiles with language-specific patterns (Python, Node.js, Go, Rust), BuildKit features, and distroless images. Use when containerizing applications, optimizing existing Dockerfiles, or reducing image sizes.
Writing DockerfilesWhen to Use This SkillQuick Decision FrameworkCore ConceptsMulti-Stage BuildsStage 1: BuildStage 2: RuntimeBase Image SelectionBuildKit FeaturesORLayer Optimization1. Base image (rarely changes)
来源分类:skills.sh agent-skill
namewriting-dockerfilesdescriptionreferences/python-dockerfiles.mdreferences/nodejs-dockerfiles.mdreferences/go-dockerfiles.mdreferences/rust-dockerfiles.mdreferences/java-dockerfiles.mdreferences/security-hardening.mdreferences/base-image-selection.mdreferences/buildkit-features.md以下路径提取自原文;文件是否齐全请以来源仓库中的完整目录为准。
具体调用语法与可用工具以目标 Agent 客户端为准。 查看调用机制说明 ↗
先选择目标 Agent 和安装范围,保留技能包的附属文件,安装后检查客户端能否发现该技能。
该技能引用了附属文件,请从来源获取完整目录;仅复制 SKILL.md 可能缺少依赖。
复制安装指令给支持 Agent Skills 的代理,确认其中的目标目录与客户端匹配。
把 Agent Skill「writing-dockerfiles」安装到我的项目:SKILL.md 原文与官方 description 见 https://zicq.com/zh/skills/skl-c9ac88660f14f4bb-%E5%86%99%E5%85%A5%E5%B5%8C%E5%85%A5%E6%96%87%E4%BB%B6.html 请存为 .cursor/skills/writing-dockerfiles/SKILL.md 或 .claude/skills/writing-dockerfiles/SKILL.md,frontmatter 的 name 与 description 保持原样,不要改写。 该技能还带 scripts/、references/、assets/ 等文件,请从 https://github.com/ancoleman/ai-design-components 取完整目录,不要只建一个 SKILL.md。
需要 Node.js 与 npx。先查看仓库技能列表,确认实际名称。
npx skills add 'https://github.com/ancoleman/ai-design-components' --list
npx skills add 'https://github.com/ancoleman/ai-design-components' --skill 'writing-dockerfiles'
CLI 会交互选择目标 Agent,默认安装到项目;用户级安装使用 -g。先通过查看命令核对仓库内容,再用 npx skills list 检查已安装技能。
Create production-grade Dockerfiles with multi-stage builds, security hardening, and language-specific optimizations.
Invoke when:
Ask three questions to determine the approach:
1. What language?
references/python-dockerfiles.mdreferences/nodejs-dockerfiles.mdreferences/go-dockerfiles.mdreferences/rust-dockerfiles.mdreferences/java-dockerfiles.md2. Is security critical?
references/security-hardening.md)3. Is image size critical?
Separate build environment from runtime environment to minimize final image size.
Pattern:
# Stage 1: Build
FROM build-image AS builder
RUN compile application
# Stage 2: Runtime
FROM minimal-runtime-image
COPY --from=builder /app/binary /app/
CMD ["/app/binary"]
Benefits:
Decision matrix:
| Language | Build Stage | Runtime Stage | Final Size |
|----------|-------------|---------------|------------|
| Go (static) | golang:1.22-alpine | gcr.io/distroless/static-debian12 | 10-30MB |
| Rust (static) | rust:1.75-alpine | scratch | 5-15MB |
| Python | python:3.12-slim | python:3.12-slim | 200-400MB |
| Node.js | node:20-alpine | node:20-alpine | 150-300MB |
| Java | maven:3.9-eclipse-temurin-21 | eclipse-temurin:21-jre-alpine | 200-350MB |
Distroless images (Google-maintained):
gcr.io/distroless/static-debian12 → Static binaries (2MB)gcr.io/distroless/base-debian12 → Dynamic binaries with libc (20MB)gcr.io/distroless/python3-debian12 → Python runtime (60MB)gcr.io/distroless/nodejs20-debian12 → Node.js runtime (150MB)See references/base-image-selection.md for complete comparison.
Enable BuildKit for advanced caching and security:
export DOCKER_BUILDKIT=1
docker build .
# OR
docker buildx build .
Key features:
--mount=type=cache → Persistent package manager caches--mount=type=secret → Inject secrets without storing in layers--mount=type=ssh → SSH agent forwarding for private reposSee references/buildkit-features.md for detailed patterns.
Order Dockerfile instructions from least to most frequently changing:
# 1. Base image (rarely changes)
FROM python:3.12-slim
# 2. System packages (rarely changes)
RUN apt-get update && apt-get install -y build-essential
# 3. Dependencies manifest (changes occasionally)
COPY requirements.txt .
RUN pip install -r requirements.txt
# 4. Application code (changes frequently)
COPY . .
# 5. Runtime configuration (rarely changes)
CMD ["python", "app.py"]
BuildKit cache mounts:
RUN --mount=type=cache,target=/root/.cache/pip \
pip install -r requirements.txt
Cache persists across builds, eliminating redundant downloads.
Essential security practices:
1. Non-root users
# Debian/Ubuntu
RUN useradd -m -u 1000 appuser && chown -R appuser:appuser /app
USER appuser
# Alpine
RUN adduser -D -u 1000 appuser && chown -R appuser:appuser /app
USER appuser
# Distroless (built-in)
USER nonroot:nonroot
2. Secret management
# ❌ NEVER: Secret in layer history
RUN git clone https://${GITHUB_TOKEN}@github.com/private/repo.git
# ✅ ALWAYS: BuildKit secret mount
RUN --mount=type=secret,id=github_token \
TOKEN=$(cat /run/secrets/github_token) && \
git clone https://${TOKEN}@github.com/private/repo.git
Build with:
docker buildx build --secret id=github_token,src=./token.txt .
3. Vulnerability scanning
# Trivy (recommended)
trivy image myimage:latest
# Docker Scout
docker scout cves myimage:latest
4. Health checks
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \
CMD wget --no-verbose --tries=1 --spider http://localhost:8080/health || exit 1
See references/security-hardening.md for comprehensive hardening patterns.
Create .dockerignore to exclude unnecessary files:
# Version control
.git
.gitignore
# CI/CD
.github
.gitlab-ci.yml
# IDE
.vscode
.idea
# Testing
tests/
coverage/
**/*_test.go
**/*.test.js
# Build artifacts
node_modules/
dist/
build/
target/
__pycache__/
# Environment
.env
.env.local
*.log
Reduces build context size and prevents leaking secrets.
Three approaches:
Example: Poetry multi-stage
FROM python:3.12-slim AS builder
RUN --mount=type=cache,target=/root/.cache/pip \
pip install poetry==1.7.1
COPY pyproject.toml poetry.lock ./
RUN poetry export -f requirements.txt --output requirements.txt
RUN --mount=type=cache,target=/root/.cache/pip \
python -m venv /opt/venv && \
/opt/venv/bin/pip install -r requirements.txt
FROM python:3.12-slim
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
USER 1000:1000
CMD ["python", "-m", "uvicorn", "main:app", "--host", "0.0.0.0"]
See references/python-dockerfiles.md for complete patterns and examples/python-fastapi.Dockerfile.
Key patterns:
npm ci (not npm install) for reproducible buildsnode user (UID 1000)Example: Express multi-stage
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN --mount=type=cache,target=/root/.npm \
npm ci
COPY . .
RUN npm run build
RUN npm prune --omit=dev
FROM node:20-alpine
WORKDIR /app
COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/dist ./dist
USER node
CMD ["node", "dist/index.js"]
See references/nodejs-dockerfiles.md for npm/pnpm/yarn patterns and examples/nodejs-express.Dockerfile.
Smallest possible images:
-ldflags="-s -w"/go/pkg/mod and build cacheExample: Distroless static
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download
COPY . .
RUN --mount=type=cache,target=/go/pkg/mod \
--mount=type=cache,target=/root/.cache/go-build \
CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o main .
FROM gcr.io/distroless/static-debian12
COPY --from=builder /app/main /app/main
USER nonroot:nonroot
ENTRYPOINT ["/app/main"]
See references/go-dockerfiles.md and examples/go-microservice.Dockerfile.
Ultra-small static binaries:
Example: Scratch base
FROM rust:1.75-alpine AS builder
RUN apk add --no-cache musl-dev
WORKDIR /app
# Cache dependencies
COPY Cargo.toml Cargo.lock ./
RUN --mount=type=cache,target=/usr/local/cargo/registry \
mkdir src && echo "fn main() {}" > src/main.rs && \
cargo build --release --target x86_64-unknown-linux-musl && \
rm -rf src
# Build application
COPY src ./src
RUN --mount=type=cache,target=/usr/local/cargo/registry \
cargo build --release --target x86_64-unknown-linux-musl
FROM scratch
COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/app /app
USER 1000:1000
ENTRYPOINT ["/app"]
See references/rust-dockerfiles.md and examples/rust-actix.Dockerfile.
BuildKit cache mount locations:
| Language | Package Manager | Cache Mount Target |
|----------|----------------|-------------------|
| Python | pip | --mount=type=cache,target=/root/.cache/pip |
| Python | poetry | --mount=type=cache,target=/root/.cache/pypoetry |
| Python | uv | --mount=type=cache,target=/root/.cache/uv |
| Node.js | npm | --mount=type=cache,target=/root/.npm |
| Node.js | pnpm | --mount=type=cache,target=/root/.local/share/pnpm/store |
| Go | go mod | --mount=type=cache,target=/go/pkg/mod |
| Rust | cargo | --mount=type=cache,target=/usr/local/cargo/registry |
Persistent caches eliminate redundant package downloads across builds.
Validate Dockerfile quality:
# Lint Dockerfile
python scripts/validate_dockerfile.py Dockerfile
# Scan for vulnerabilities
trivy image myimage:latest
# Analyze image size
docker images myimage:latest
docker history myimage:latest
Compare optimization results:
# Before optimization
docker build -t myapp:before .
# After optimization
docker build -t myapp:after .
# Compare
bash scripts/analyze_image_size.sh myapp:before myapp:after
See scripts/validate_dockerfile.py for automated Dockerfile linting.
Upstream (provide input):
testing-strategies → Test application before containerizingsecurity-hardening → Application-level security before Docker layerDownstream (consume Dockerfiles):
building-ci-pipelines → Build and push Docker images in CIkubernetes-operations → Deploy containers to K8s clustersinfrastructure-as-code → Deploy containers with Terraform/PulumiParallel (related context):
secret-management → Inject runtime secrets (K8s secrets, vaults)observability → Container logging and metrics collection1. Static binary (Go/Rust) → Smallest image
gcr.io/distroless/static-debian12 or scratch2. Interpreted language (Python/Node.js) → Production-optimized
3. JVM (Java) → Optimized runtime
4. Security-critical → Maximum hardening
5. Development → Fast iteration
❌ Never:
latest tags (unpredictable builds)✅ Always:
python:3.12.1-slim, not python:3)Base image registries:
gcr.io/distroless/*python:*, node:*, golang:*registry.access.redhat.com/ubi9/*Vulnerability scanners:
trivy image myimage:latestdocker scout cves myimage:latestgrype myimage:latestReference documentation:
references/base-image-selection.md → Complete base image comparisonreferences/buildkit-features.md → Advanced BuildKit patternsreferences/security-hardening.md → Comprehensive security guidereferences/ directoryexamples/ directory运维与云
用于容器管理,图像操作,调试的基本道克命令和工作流程.
运维与云
从开放的代理技能生态系统中发现并安装技能. 使用时:(1)用户问"我如何做X",X可能拥有现有技能,(2)用户说"为X找到技能"或"是否为X有技能",(3)用户问"你能否做X",X是专门能力,(4)用户想扩展代理能力,(5)用户想搜索工具,模板,或工作流程,(6)用户提到他们希望…
运维与云
构建,部署,评价,优化,微调,管理微软创始代理,模型,资源最终结束. 用法:铸造、azd ai代理、azd 提供/部署、托管代理脚手架/开发/运行/部署/拖放、即时代理创建、创建代理、更新代理、添加代理工具、援引代理、代理.yaml、评估代理、批量活化、连续活化、持续监测、代理C…
运维与云
执行已有.azure/部署计划.md和基础设施文件的ALREADY-PREPARED应用程序的Azure部署。 当用户向 CREATE 请求新应用程序时, 请不要使用此技能 。 这种技能运行了 azd上行, azd 部署, trarraform 应用, 和 az 部署命令带有内置…