Docker Development
Smaller images. Faster builds. Secure containers. No guesswork.
Opinionated Docker workflow that turns bloated Dockerfiles into production-grade containers. Covers optimization, multi-stage builds, compose orchestration, and security hardening.
Not a Docker tutorial — a set of concrete decisions about how to build containers that don't waste time, space, or attack surface.
Slash Commands
| Command | What it does |
|---|
| INLINECODE0 | Analyze and optimize a Dockerfile for size, speed, and layer caching |
| INLINECODE1 |
Generate or improve docker-compose.yml with best practices |
|
/docker:security | Audit a Dockerfile or running container for security issues |
When This Skill Activates
Recognize these patterns from the user:
- - "Optimize this Dockerfile"
- "My Docker build is slow"
- "Create a docker-compose for this project"
- "Is this Dockerfile secure?"
- "Reduce my Docker image size"
- "Set up multi-stage builds"
- "Docker best practices for [language/framework]"
- Any request involving: Dockerfile, docker-compose, container, image size, build cache, Docker security
If the user has a Dockerfile or wants to containerize something → this skill applies.
Workflow
/docker:optimize — Dockerfile Optimization
- 1. Analyze current state
- Read the Dockerfile
- Identify base image and its size
- Count layers (each RUN/COPY/ADD = 1 layer)
- Check for common anti-patterns
- 2. Apply optimization checklist
CODEBLOCK0
- 3. Generate optimized Dockerfile
- Apply all relevant optimizations
- Add inline comments explaining each decision
- Report estimated size reduction
- 4. Validate
CODEBLOCK1
/docker:compose — Docker Compose Configuration
- 1. Identify services
- Application (web, API, worker)
- Database (postgres, mysql, redis, mongo)
- Cache (redis, memcached)
- Queue (rabbitmq, kafka)
- Reverse proxy (nginx, traefik, caddy)
- 2. Apply compose best practices
CODEBLOCK2
- 3. Generate compose file
- Output docker-compose.yml with healthchecks, networks, volumes
- Generate .env.example with all required variables documented
- Add dev/prod profile annotations
/docker:security — Container Security Audit
- 1. Dockerfile audit
| Check | Severity | Fix |
|-------|----------|-----|
| Running as root | Critical | Add USER nonroot after creating user |
| Using :latest tag | High | Pin to specific version |
| Secrets in ENV/ARG | Critical | Use BuildKit secrets: --mount=type=secret |
| COPY with broad glob | Medium | Use specific paths, add .dockerignore |
| Unnecessary EXPOSE | Low | Only expose ports the app uses |
| No HEALTHCHECK | Medium | Add HEALTHCHECK with appropriate interval |
| Privileged instructions | High | Avoid --privileged, drop capabilities |
| Package manager cache retained | Low | Clean in same RUN layer |
- 2. Runtime security checks
| Check | Severity | Fix |
|-------|----------|-----|
| Container running as root | Critical | Set user in Dockerfile or compose |
| Writable root filesystem | Medium | Use read_only: true in compose |
| All capabilities retained | High | Drop all, add only needed: cap_drop: [ALL] |
| No resource limits | Medium | Set mem_limit and cpus |
| Host network mode | High | Use bridge or custom network |
| Sensitive mounts | Critical | Never mount /etc, /var/run/docker.sock in prod |
| No log driver configured | Low | Set logging: with size limits |
- 3. Generate security report
SECURITY AUDIT — [Dockerfile/Image name]
Date: [timestamp]
CRITICAL: [count]
HIGH: [count]
MEDIUM: [count]
LOW: [count]
[Detailed findings with fix recommendations]
Tooling
scripts/dockerfile_analyzer.py
CLI utility for static analysis of Dockerfiles.
Features:
- - Layer count and optimization suggestions
- Base image analysis with size estimates
- Anti-pattern detection (15+ rules)
- Security issue flagging
- Multi-stage build detection and validation
- JSON and text output
Usage:
CODEBLOCK4
scripts/compose_validator.py
CLI utility for validating docker-compose files.
Features:
- - Service dependency validation
- Healthcheck presence detection
- Network configuration analysis
- Volume mount validation
- Environment variable audit
- Port conflict detection
- Best practice scoring
Usage:
# Validate a compose file
python3 scripts/compose_validator.py docker-compose.yml
# JSON output
python3 scripts/compose_validator.py docker-compose.yml --output json
# Strict mode (fail on warnings)
python3 scripts/compose_validator.py docker-compose.yml --strict
Multi-Stage Build Patterns
Pattern 1: Compiled Language (Go, Rust, C++)
CODEBLOCK6
Pattern 2: Node.js / TypeScript
CODEBLOCK7
Pattern 3: Python
CODEBLOCK8
Base Image Decision Tree
CODEBLOCK9
Proactive Triggers
Flag these without being asked:
- - Dockerfile uses :latest → Suggest pinning to a specific version tag.
- No .dockerignore → Create one. At minimum:
.git, node_modules, __pycache__, .env. - COPY . . before dependency install → Cache bust. Reorder to install deps first.
- Running as root → Add USER instruction. No exceptions for production.
- Secrets in ENV or ARG → Use BuildKit secret mounts. Never bake secrets into layers.
- Image over 1GB → Multi-stage build required. No reason for a production image this large.
- No healthcheck → Add one. Orchestrators (Compose, K8s) need it for proper lifecycle management.
- apt-get without cleanup in same layer →
rm -rf /var/lib/apt/lists/* in the same RUN.
Installation
One-liner (any tool)
CODEBLOCK10
Multi-tool install
CODEBLOCK11
OpenClaw
clawhub install cs-docker-development
Related Skills
- - senior-devops — Broader DevOps scope (CI/CD, IaC, monitoring). Complementary — use docker-development for container-specific work, senior-devops for pipeline and infrastructure.
- senior-security — Application security. Complementary — docker-development covers container security, senior-security covers application-level threats.
- autoresearch-agent — Can optimize Docker build times or image sizes as measurable experiments.
- ci-cd-pipeline-builder — Pipeline construction. Complementary — docker-development builds the containers, ci-cd-pipeline-builder deploys them.
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 优化
- 1. 分析当前状态
- 读取 Dockerfile
- 识别基础镜像及其大小
- 计算层数(每个 RUN/COPY/ADD = 1 层)
- 检查常见的反模式
- 2. 应用优化检查清单
基础镜像
├── 使用特定标签,生产环境永远不要用 :latest
├── 优先选择 slim/alpine 变体(debian-slim > ubuntu > debian)
├── 在 CI 中固定摘要以保证可重现性:image@sha256:...
└── 根据运行时需求匹配基础镜像(不要为编译后的二进制文件使用 python:3.12)
层优化
├── 使用 && \ 合并相关的 RUN 命令
├── 排序层:最不常变化的在前(依赖项在源代码之前)
├── 在同一个 RUN 层中清理包管理器缓存
├── 使用 .dockerignore 排除不必要的文件
└── 分离构建依赖项和运行时依赖项
构建缓存
├── 在源代码之前复制依赖文件(package.json、requirements.txt、go.mod)
├── 在与代码复制不同的层中安装依赖项
├── 使用 BuildKit 缓存挂载:--mount=type=cache,target=/root/.cache
└── 避免在依赖安装之前使用 COPY . .
多阶段构建
├── 阶段 1:构建(完整 SDK、构建工具、开发依赖项)
├── 阶段 2:运行时(最小基础镜像,仅生产工件)
├── COPY --from=builder 仅复制所需内容
└── 最终镜像不应包含构建工具、源代码或开发依赖项
- 3. 生成优化的 Dockerfile
- 应用所有相关优化
- 添加内联注释解释每个决策
- 报告预估的大小缩减
- 4. 验证
bash
python3 scripts/dockerfile_analyzer.py Dockerfile
/docker:compose — Docker Compose 配置
- 1. 识别服务
- 应用程序(Web、API、Worker)
- 数据库(Postgres、MySQL、Redis、Mongo)
- 缓存(Redis、Memcached)
- 队列(RabbitMQ、Kafka)
- 反向代理(Nginx、Traefik、Caddy)
- 2. 应用 Compose 最佳实践
服务
├── 使用 dependson 配合 condition: servicehealthy
├── 为每个服务添加健康检查
├── 设置资源限制(mem_limit、cpus)
├── 使用命名卷存储持久数据
└── 固定镜像版本
网络
├── 创建显式网络(不要依赖默认网络)
├── 分离前端和后端网络
├── 仅暴露需要外部访问的端口
└── 对仅后端网络使用 internal: true
环境
├── 使用 env_file 存储密钥,不要内联在 environment 中
├── 永远不要提交 .env 文件(添加到 .gitignore)
├── 使用变量替换:${VAR:-default}
└── 记录所有必需的环境变量
开发 vs 生产
├── 使用 Compose 配置文件或覆盖文件
├── 开发:绑定挂载实现热重载,暴露调试端口
├── 生产:命名卷,无调试端口,restart: unless-stopped
└── docker-compose.override.yml 用于仅开发配置
- 3. 生成 Compose 文件
- 输出包含健康检查、网络、卷的 docker-compose.yml
- 生成包含所有必需变量文档的 .env.example
- 添加开发/生产配置文件注释
/docker:security — 容器安全审计
- 1. Dockerfile 审计
| 检查项 | 严重程度 | 修复方法 |
|-------|----------|-----|
| 以 root 用户运行 | 严重 | 创建用户后添加 USER nonroot |
| 使用 :latest 标签 | 高 | 固定到特定版本 |
| ENV/ARG 中的密钥 | 严重 | 使用 BuildKit 密钥:--mount=type=secret |
| 使用宽泛通配符的 COPY | 中 | 使用特定路径,添加 .dockerignore |
| 不必要的 EXPOSE | 低 | 仅暴露应用使用的端口 |
| 无 HEALTHCHECK | 中 | 添加带有适当间隔的 HEALTHCHECK |
| 特权指令 | 高 | 避免 --privileged,删除能力 |
| 保留包管理器缓存 | 低 | 在同一个 RUN 层中清理 |
- 2. 运行时安全检查
| 检查项 | 严重程度 | 修复方法 |
|-------|----------|-----|
| 容器以 root 用户运行 | 严重 | 在 Dockerfile 或 Compose 中设置用户 |
| 可写的根文件系统 | 中 | 在 Compose 中使用 read_only: true |
| 保留所有能力 | 高 | 删除所有,仅添加需要的:cap_drop: [ALL] |
| 无资源限制 | 中 | 设置 mem_limit 和 cpus |
| 主机网络模式 | 高 | 使用桥接或自定义网络 |
| 敏感挂载 | 严重 | 生产环境中永远不要挂载 /etc、/var/run/docker.sock |
| 未配置日志驱动 | 低 | 设置带有大小限制的 logging: |
- 3. 生成安全报告
安全审计 — [Dockerfile/镜像名称]
日期:[时间戳]
严重:[数量]
高: [数量]
中: [数量]
低: [数量]
[详细发现及修复建议]
工具
scripts/dockerfile_analyzer.py
用于 Dockerfile 静态分析的 CLI 工具。
功能:
- - 层数统计和优化建议
- 基础镜像分析及大小估算
- 反模式检测(15+ 条规则)
- 安全问题标记
- 多阶段构建检测和验证
- JSON 和文本输出
用法:
bash
分析 Dockerfile
python3 scripts/dockerfile_analyzer.py Dockerfile
JSON 输出
python3 scripts/dockerfile_analyzer.py Dockerfile --output json
以安全为重点进行分析
python3 scripts/dockerfile_analyzer.py Dockerfile --security
检查特定目录
python3 scripts/dockerfile_analyzer.py path/to/Dockerfile
scripts/compose_validator.py
用于验证 docker-compose 文件的 CLI 工具。
功能:
- - 服务依赖验证
- 健康检查存在性检测
- 网络配置分析
- 卷挂载验证
- 环境变量审计
- 端口冲突检测
- 最佳实践评分
用法:
bash
验证 Compose 文件
python3 scripts/compose_validator.py docker-compose.yml
JSON 输出
python3 scripts/compose_validator.py docker-compose.yml --output json
严格模式(警告时失败)
python3 scripts/compose_validator.py docker-compose.yml --strict
多阶段构建模式
模式 1:编译型语言(Go、Rust、C++)
dockerfile
构建阶段
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.mod go.sum ./
RUN go