Docker 助手
- 作者仓库星标 1
- 作者更新于 实时读取
- 作者仓库 docker-best-practices-skill
- 领域
- 运维部署
- 兼容 Agent
-
- Claude Code
- Cursor
- Cline
- Codex
- Windsurf
- Gemini CLI
- +20
- 信任分
- 83 / 100 · 社区维护
- 作者 / 版本 / 许可
- @yogapemoy · 未声明 license
- Token 消耗评级
- 低消耗
- 接入复杂程度
- 需手动接入
- 是否需要外部 API Key
- 不需要
- 兼容的系统
- Linux · Docker
- 底层运行要求
- Node.js · Python · Docker
- 文件与系统权限
-
- 只读
- 允许写入 / 修改
- Shell 执行
- 读取环境变量
- 网络行为
- 允许外网请求
- 安装命令数
- 26 条
档案由构建时根据 SKILL.md 与安装命令自动衍生,可能与作者实际意图存在差异。
需要注意: 未限定 allowed-tools,默认拥有全部工具权限。;检出高风险片段:rm_rf_root
---
name: docker-best-practices
description: Docker 多服务部署最佳实践 - 遵循生产级 Dockerfile 和 Docker Compose 架构原则。核心原则:Dockerfile 不随环境变化、compose 不包含真实 s…
category: 运维部署
runtime: Node.js / Python / Docker
---
# docker-best-practices 输出预览
## PART A: 任务判断
- 适用问题:部署、CI、环境检查、发布或运维排障。
- 输入要求:目标材料、限制条件、期望输出和验收方式。
- 证据边界:围绕“When to Use This Skill / 核心架构运行哲学 / Dockerfile 构建五原则”读取原文规则,不把推断写成作者承诺。
## PART B: 执行结果
- **01** 任务判断:确认你的需求是否属于部署、CI、环境检查、发布或运维排障,并标出输入、限制和预期结果。
- **02** 执行计划:优先按“When to Use This Skill / 核心架构运行哲学 / Dockerfile 构建五原则”拆成步骤,说明每一步会读取什么、修改什么、产出什么。
- **03** 交付结果:给出可复制的命令、文件改动、检查清单或内容草稿,并说明如何继续迭代。
- **04** 风险边界:结合 读取文件、写入/修改文件、执行终端命令、读取环境变量、会按任务需要访问外部网络、通常不需要额外 API Key 给出执行前确认项。
## Running Rules
- 读取文件、写入/修改文件、执行终端命令、读取环境变量;会按任务需要访问外部网络;通常不需要额外 API Key。
- 先小样例验证,再放大到真实任务。
- 交付时同时给结果、检查口径和下一步迭代建议。 原文出现了 `/app`、`/var`、`/backup`、`/data` 这类斜杠命令;如果你的 Agent 支持命令触发,优先用命令开场,再补充目标和边界。
告诉 Agent 目标文件或材料、期望结果、不可改范围、是否允许联网或执行命令。本 Skill 的权限画像是:读取文件、写入/修改文件、执行终端命令、读取环境变量。
先用一个小任务确认它会围绕“When to Use This Skill / 核心架构运行哲学 / Dockerfile 构建五原则”工作;涉及文件或命令时,先看 diff、日志、预览或测试结果。
检查最终产物是否包含明确结果、必要证据和下一步动作;如果输出泛泛而谈,就补充输入、边界和验收标准后重跑。
---
name: docker-best-practices
description: Docker 多服务部署最佳实践 - 遵循生产级 Dockerfile 和 Docker Compose 架构原则。核心原则:Dockerfile 不随环境变化、compose 不包含真实 s…
category: 运维部署
source: yogapemoy/docker-best-practices-skill
---
# docker-best-practices
## 什么时候使用
- 把部署运维方向的常用动作沉淀成 Agent 可调用的技能 适合处理部署、CI、发布、回滚、环境检查和运维排障,核心价值是把输入、判断、执行、验证和交付边界固定下来,避免 Agent 泛泛回答。 把任务拆成可执行、可检查、可继续迭代的步骤…
- 面向部署、CI、环境检查、发布或运维排障,优先处理能明确输入、步骤和验收标准的工作。
## 需要提供什么
- 目标材料、目录范围、期望结果和不可改动内容。
- 是否允许联网、执行命令、读写文件或调用外部服务。
## 执行规则
- 围绕「When to Use This Skill / 核心架构运行哲学 / Dockerfile 构建五原则」组织步骤,不把推断写成作者事实。
- 读取文件、写入/修改文件、执行终端命令、读取环境变量;会按任务需要访问外部网络;通常不需要额外 API Key。
- 先跑小样例,确认结果可检查后再扩大任务范围。
## 输出要求
- 给出最终产物、关键证据、验证方式和下一步动作。
- 信息不足时标记 unknown,不编造命令、平台或依赖。 作者原文负责流程事实;仓库文件负责来源和命令;流狐只补充适用场景、限制和质量判断。
skill "docker-best-practices" {
输入层 -> 用户目标 + 目标文件 + 禁止范围 + 验收标准
上下文层 -> When to Use This Skill / 核心架构运行哲学 / Dockerfile 构建五原则
规则层 -> SKILL.md 触发条件 / 执行顺序 / 输出格式
运行层 -> Node.js / Python / Docker | 读取文件、写入/修改文件、执行终端命令、读取环境变量 | 会按任务需要访问外部网络
安全层 -> 通常不需要额外 API Key + 小任务验证 + diff / 日志复核
输出层 -> 可复制结果 + 检查清单 + 下一步迭代
} Docker-Best-Practices Skill
Docker 多服务部署最佳实践 - 生产级 Dockerfile 和 Docker Compose 架构指南。
When to Use This Skill
This skill should be triggered when:
- Designing multi-service Docker architectures for production deployment
- Writing or optimizing Dockerfiles for size, security, and performance
- Configuring Docker Compose for multi-container applications
- Implementing data persistence strategies with volumes and mounts
- Setting up environment-specific configurations using .env files
- Implementing container health checks and monitoring
- Securing Docker containers with proper user permissions and capabilities
- Optimizing container resource usage (CPU, memory, I/O limits)
- Deploying applications from development to production with Docker
- Troubleshooting Docker deployment issues related to networking, storage, or configuration
核心架构运行哲学
必须遵守的 5 条原则:
Dockerfile 不随环境变化
- Dockerfile 只定义"构建什么",不定义"运行时配置"
- 所有环境差异(dev/staging/prod)通过
.env文件注入 - 同一个 Dockerfile 应该能在所有环境使用
Compose 不包含真实 secret
compose.yaml只包含 secret 的占位符或引用- 真实密钥从
.env、secret manager 或挂载文件读取 - 永远不要将
compose.yaml包含的 secret 提交到版本控制
所有环境差异 →
.env- 环境变量优先级:Shell > --env-file > .env > compose defaults
- 每个环境有独立的
.env.{environment}文件 .env.example提供模板,但不包含真实值
容器可销毁,数据不可
- 容器是 ephemeral(临时的),随时可以删除重建
- 数据必须持久化到 volumes 或外部存储
- 状态与容器分离,重建不影响数据
恢复能力 > 自动化炫技
- 优先考虑故障恢复和数据安全
- 避免过度复杂的自动化脚本
- 简单的备份/恢复流程胜过复杂的编排
Dockerfile 构建五原则
必须遵循的 5 条构建原则:
为合成而构建,把不变的放在易变的前面
# 不变的底层依赖(很少变化) FROM python:3.12-slim AS base WORKDIR /app # 依赖安装(偶尔变化) COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt # 应用代码(经常变化) COPY . .分离构建和运行,让最终的产物绝对纯粹
# 构建阶段:包含所有构建工具 FROM node:20 AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build # 运行阶段:只包含运行时必需 FROM node:20-slim COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules USER node CMD ["node", "dist/server.js"]最小权限运行,永远不要相信默认设置
FROM alpine:3.19 # 创建非 root 用户 RUN addgroup -g 1000 appgroup && \ adduser -D -u 1000 -G appgroup appuser # 只开放必需的端口 EXPOSE 8080 # 以非 root 用户运行 USER appuser # 删除不必要的 capabilities # 运行时:--cap-drop=ALL --cap-add=NET_BIND_SERVICE自动化健康检查,赋予系统治愈的能力
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3 \ CMD curl -f http://localhost:8080/health || exit 1 # 或使用自定义脚本 HEALTHCHECK --interval=30s --timeout=5s --retries=3 \ CMD /app/scripts/healthcheck.sh凡事都要明确,清晰的声明工作目录、暴露的端口和入口点
# 明确的基础镜像版本 FROM python:3.12-slim # 明确的工作目录 WORKDIR /app # 明确的环境变量 ENV PYTHONUNBUFFERED=1 \ PYTHONDONTWRITEBYTECODE=1 \ PATH="/app/bin:${PATH}" # 明确的暴露端口 EXPOSE 8080/tcp # 明确的卷挂载点 VOLUME ["/app/data", "/app/logs"] # 明确的入口点 ENTRYPOINT ["python", "-m", "myapp"] CMD ["--config", "/app/config/config.yaml"]
Quick Reference
Dockerfile Best Practices
1. Use multi-stage builds for size reduction
# syntax=docker/dockerfile:1
FROM node:20 AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
FROM node:20-slim
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
CMD ["node", "dist/server.js"]
Source: Official Docker documentation - Multi-stage builds
2. Choose minimal, trusted base images
# Good: Official slim images
FROM python:3.12-slim
# Better: Alpine-based (even smaller)
FROM python:3.12-alpine
# Best: distroless for production (minimal, auto-updates)
FROM gcr.io/distroless/python3-debian12
Source: Official Docker best practices
3. Combine RUN commands to reduce layers
# Bad: Multiple layers
RUN apt-get update
RUN apt-get install -y python3
RUN apt-get clean
# Good: Single layer with cleanup
RUN apt-get update && \
apt-get install -y --no-install-recommends python3 && \
rm -rf /var/lib/apt/lists/*
Source: Official Docker best practices
4. Use .dockerignore to exclude unnecessary files
# .dockerignore
node_modules
npm-debug.log
.git
.env.local
*.md
tests/
Source: Official Docker documentation
5. Run as non-root user
RUN adduser --disabled-password --gecos '' appuser
USER appuser
Source: Official Docker security documentation
6. Implement health checks
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
CMD curl -f http://localhost:8080/health || exit 1
Source: Official Docker documentation
7. Use specific versions, not latest
# Bad: Unpredictable updates
FROM ubuntu:latest
# Good: Reproducible builds
FROM ubuntu:24.04
Source: Official Docker best practices
Docker Compose Patterns
8. Environment variable interpolation
# compose.yaml
services:
web:
image: "webapp:${TAG:-latest}"
environment:
- DATABASE_URL=${DATABASE_URL}
- DEBUG=${DEBUG:-false}
# .env
TAG=v1.5
DATABASE_URL=postgres://db:5432/app
Source: Official Docker Compose documentation
9. Multi-environment configuration
# compose.base.yaml (common configuration)
services:
web:
build: .
ports:
- "8000:8000"
# compose.production.yaml (production overrides)
services:
web:
restart: always
environment:
- NODE_ENV=production
deploy:
resources:
limits:
cpus: '0.50'
memory: 512M
# Development
docker compose up
# Production
docker compose -f compose.base.yaml -f compose.production.yaml up -d
Source: Official Docker Compose production guide
10. Volume persistence with named volumes
services:
db:
image: postgres:16
volumes:
- pgdata:/var/lib/postgresql/data
environment:
- POSTGRES_PASSWORD=${DB_PASSWORD}
volumes:
pgdata:
# Named volume persists across container restarts
Source: Official Docker volumes documentation
11. Using secrets in Compose
services:
web:
image: myapp:latest
secrets:
- db_password
environment:
- DB_PASSWORD_FILE=/run/secrets/db_password
secrets:
db_password:
file: ./secrets/db_password.txt
Source: Official Docker security documentation
Data Persistence Patterns
12. Volume for persistent data
# Create and use a named volume
docker volume create mydata
docker run -d --name app \
--mount source=mydata,target=/app/data \
myapp:latest
Source: Official Docker volumes documentation
13. Bind mount for development
# Sync local files into container
docker run -d --name dev \
--mount type=bind,source=$(pwd)/src,target=/app/src \
myapp:latest
Source: Official Docker documentation
14. Read-only volume for shared data
services:
web:
image: nginx:alpine
volumes:
- static_content:/usr/share/nginx/html:ro
volumes:
static_content:
Source: Official Docker volumes documentation
Container Security
15. Drop unnecessary capabilities
docker run -d --name secure_app \
--cap-drop=ALL \
--cap-add=NET_BIND_SERVICE \
myapp:latest
Source: Official Docker security documentation
16. Run with resource limits
docker run -d --name app \
--memory="512m" \
--memory-reservation="256m" \
--cpus="0.5" \
--pids-limit 100 \
myapp:latest
Source: Official Docker run documentation
17. Run as non-root
docker run -d --name app \
--user 1000:1000 \
myapp:latest
Source: Official Docker security documentation
Production Deployment
18. Health check in docker run
docker run -d --name app \
--health-cmd="curl -f http://localhost/health || exit 1" \
--health-interval=30s \
--health-timeout=3s \
--health-retries=3 \
myapp:latest
Source: Official Docker run documentation
19. Update and redeploy a service
# Rebuild and recreate without affecting dependencies
docker compose up -d --build web
Source: Official Docker Compose production guide
20. Backup and restore volumes
# Backup a volume
docker run --rm --volumes-from app_container \
-v $(pwd):/backup \
alpine tar czf /backup/backup.tar.gz /data
# Restore to new container
docker run --rm --volumes-from new_app_container \
-v $(pwd):/backup \
alpine tar xzf /backup/backup.tar.gz
Source: Official Docker volumes documentation
Reference Files
This skill includes comprehensive documentation in references/:
Core Documentation
getting_started.md (4 pages, confidence: medium)
- Dockerfile best practices
- Multi-stage builds
- Base image selection
- Environment variable management
- Rebuilding and caching strategies
.dockerignoreusage
dockerfile.md (2 pages, confidence: medium)
- Multi-stage build syntax
- Build stage naming and targeting
- External image usage
- BuildKit vs legacy builder differences
- Complete Dockerfile instruction reference
compose.md (5 pages, confidence: medium)
- Environment variable interpolation
- .env file configuration
- Variable precedence rules
- Volume management in Compose
- Production deployment patterns
security.md (1 page, confidence: medium)
- Container security configuration
- Linux capabilities management
- User permissions
- Resource constraints
- Health check parameters
other.md (1 page, confidence: medium)
- Docker run command reference
- Container lifecycle management
- Networking basics
- Filesystem mounts
Usage Guidance
Use these reference files when you need:
- Detailed syntax: Check the appropriate reference file for complete syntax
- Examples: Each reference contains real-world examples from official docs
- Troubleshooting: Reference files contain common issues and solutions
- Best practices: Official recommendations for production deployments
Working with This Skill
For Beginners
- Start with the concepts: Understand the core principles before diving into syntax
- Use the quick reference: The examples above cover the most common patterns
- Check reference files: When you need detailed information about specific commands
For Intermediate Users
- Study multi-stage builds: Essential for production-grade images
- Learn Compose patterns: Critical for multi-service applications
- Understand volumes: Data persistence is key to production deployments
- Implement security: Never run production containers without security hardening
For Advanced Users
- Optimize build caching: Structure Dockerfiles to maximize cache hits
- Resource management: Set appropriate CPU, memory, and I/O limits
- Multi-environment configs: Use Compose overrides for dev/staging/prod
- Health monitoring: Implement comprehensive health checks and logging
- Secrets management: Use proper secret handling, never commit credentials
Common Workflows
Building a production image:
- Start with a minimal base image
- Use multi-stage builds to reduce size
- Combine RUN commands to minimize layers
- Add health checks
- Run as non-root user
- Test locally before pushing
Deploying with Compose:
- Create base
compose.yamlwith service definitions - Use
.envfor environment-specific values - Create override files (e.g.,
compose.production.yaml) for different environments - Use named volumes for persistent data
- Never commit secrets to version control
Managing data:
- Use named volumes for persistent data
- Use bind mounts for development (hot reload)
- Implement backup strategies for volumes
- Document volume contents and purpose
Notes
- Source: Official Docker documentation (docs.docker.com)
- Coverage: Dockerfiles, Docker Compose, volumes, security, and deployment
- Best practices: All examples follow official Docker recommendations
- Multi-environment: Patterns for dev, staging, and production
- Security: Emphasis on secure configurations and secrets management
Key Takeaways
- Separate concerns: Dockerfiles (build), Compose (orchestration), .env (configuration)
- Multi-stage builds: Use them to create small, secure production images
- Never commit secrets: Use environment variables and secret management
- Containers are ephemeral: Design with data persistence in mind
- Security by default: Minimal images, non-root users, dropped capabilities
- Reproducibility: Pin image versions, use specific tags, document dependencies
- Health checks: Always implement health checks for production services
- Resource limits: Set CPU, memory, and I/O limits to prevent runaway containers
Updating
To refresh this skill with updated documentation:
- Re-run the scraper with the same configuration
- The skill will be rebuilt with the latest information from official Docker documentation
先判断是否适合
作者设计意图
作者的方法与取舍
边界和复核