
使用方式
关于
Docker 和容器开发 Agent Skill,用于 Dockerfile 优化、docker-compose 编排、多阶段构建和容器安全加固。适用于优化 Dockerfile、创建或改进 docker-compose 配置、实现多阶段构建、审计容器安全等场景。
Docker 开发
更小的镜像。更快的构建。安全的容器。无需猜测。
一套有主见的 Docker 工作流,将臃肿的 Dockerfile 转化为生产级容器。涵盖优化、多阶段构建、Compose 编排和安全加固。
这不是 Docker 教程——而是一组关于如何构建不浪费时间、空间和攻击面的容器的具体决策。
斜杠命令
| 命令 | 功能 |
|---------|-------------|
| /docker:optimize | 分析并优化 Dockerfile 的大小、速度和层缓存 |
| /docker:compose | 生成或改进符合最佳实践的 docker-compose.yml |
| /docker:security | 审计 Dockerfile 或运行中容器的安全问题 |
技能激活条件
识别以下用户模式:
- "优化这个 Dockerfile"
- "我的 Docker 构建很慢"
- "为这个项目创建 docker-compose"
- "这个 Dockerfile 安全吗?"
- "减小我的 Docker 镜像大小"
- "设置多阶段构建"
- "针对 [语言/框架] 的 Docker 最佳实践"
- 任何涉及以下内容的请求:Dockerfile、docker-compose、容器、镜像大小、构建缓存、Docker 安全
如果用户有 Dockerfile 或想要容器化某些东西 → 此技能适用。
工作流
/docker:optimize — Dockerfile 优化
-
分析当前状态
- 读取 Dockerfile
- 识别基础镜像及其大小
- 计算层数(每个 RUN/COPY/ADD = 1 层)
- 检查常见反模式
-
应用优化清单
BASE IMAGE ├── Use specific tags, never :latest in production ├── Prefer slim/alpine variants (debian-slim > ubuntu > debian) ├── Pin digest for reproducibility in CI: image@sha256:... └── Match base to runtime needs (don't use python:3.12 for a compiled binary) LAYER OPTIMIZATION ├── Combine related RUN commands with && \ ├── Order layers: least-changing first (deps before source code) ├── Clean package manager cache in the same RUN layer ├── Use .dockerignore to exclude unnecessary files └── Separate build deps from runtime deps BUILD CACHE ├── COPY dependency files before source code (package.json, requirements.txt, go.mod) ├── Install deps in a separate layer from code copy ├── Use BuildKit cache mounts: --mount=type=cache,target=/root/.cache └── Avoid COPY . . before dependency installation MULTI-STAGE BUILDS ├── Stage 1: build (full SDK, build tools, dev deps) ├── Stage 2: runtime (minimal base, only production artifacts) ├── COPY --from=builder only what's needed └── Final image should have NO build tools, NO source code, NO dev deps -
生成优化后的 Dockerfile
- 应用所有相关优化
- 添加内联注释解释每个决策
- 报告预估的大小缩减
-
验证
python3 scripts/dockerfile_analyzer.py Dockerfile
/docker:compose — Docker Compose 配置
-
识别服务
- 应用程序(web、API、worker)
- 数据库(postgres、mysql、redis、mongo)
- 缓存(redis、memcached)
- 队列(rabbitmq、kafka)
- 反向代理(nginx、traefik、caddy)
-
应用 Compose 最佳实践
SERVICES ├── Use depends_on with condition: service_healthy ├── Add healthchecks for every service ├── Set resource limits (mem_limit, cpus) ├── Use named volumes for persistent data └── Pin image versions NETWORKING ├── Create explicit networks (don't rely on default) ├── Separate frontend and backend networks ├── Only expose ports that need external access └── Use internal: true for backend-only networks ENVIRONMENT ├── Use env_file for secrets, not inline environment ├── Never commit .env files (add to .gitignore) ├── Use variable substitution: ${VAR:-default} └── Document all required env vars DEVELOPMENT vs PRODUCTION ├── Use compose profiles or override files ├── Dev: bind mounts for hot reload, debug ports exposed ├── Prod: named volumes, no debug ports, restart: unless-stopped └── docker-compose.override.yml for dev-only config -
生成 Compose 文件
- 输出包含健康检查、网络、卷的 docker-compose.yml
- 生成 .env.example 并记录所有必需变量
- 添加开发/生产 profile 注解
/docker:security — 容器安全审计
-
Dockerfile 审计
| 检查项 | 严重程度 | 修复方案 | |-------|----------|-----| | 以 root 运行 | 严重 | 创建用户后添加
USER nonroot| | 使用 :latest 标签 | 高 | 固定到特定版本 | | 在 ENV/ARG 中存储密钥 | 严重 | 使用 BuildKit secrets:--mount=type=secret| | 使用宽泛通配符的 COPY | 中 | 使用特定路径,添加 .dockerignore | | 不必要的 EXPOSE | 低 | 仅暴露应用使用的端口 | | 无 HEALTHCHECK | 中 | 添加适当间隔的 HEALTHCHECK | | 特权指令 | 高 | 避免--privileged,删除能力 | -
运行时安全
- 检查容器是否以非 root 用户运行
- 验证只读文件系统设置
- 审计挂载的卷和权限
- 检查网络暴露面
-
生成安全报告
- 按严重程度排序的问题列表
- 每个问题的具体修复命令
- 加固后的 Dockerfile 示例


