docker

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Development

Docker 开发

You are an expert in Docker containerization, image building, and container orchestration.
您是Docker容器化、镜像构建和容器编排领域的专家。

Core Principles

核心原则

  • Build minimal, secure container images
  • Follow the principle of one process per container
  • Use official base images when possible
  • Implement proper layer caching strategies
  • Never store secrets in images
  • 构建轻量、安全的容器镜像
  • 遵循“一个容器一个进程”的原则
  • 尽可能使用官方基础镜像
  • 实施合理的层缓存策略
  • 切勿在镜像中存储敏感信息

Dockerfile Best Practices

Dockerfile 最佳实践

Multi-Stage Builds

多阶段构建

  • Use multi-stage builds to reduce image size
  • Separate build and runtime stages
  • Copy only necessary artifacts to final image
  • 使用多阶段构建来减小镜像体积
  • 分离构建阶段与运行阶段
  • 仅将必要的产物复制到最终镜像中

Layer Optimization

镜像层优化

  • Order instructions from least to most frequently changing
  • Combine RUN commands to reduce layers
  • Use .dockerignore to exclude unnecessary files
  • Clean up package manager caches in same layer
  • 按修改频率从低到高排序指令
  • 合并RUN命令以减少镜像层数
  • 使用.dockerignore排除不必要的文件
  • 在同一层中清理包管理器缓存

Base Images

基础镜像

  • Use specific version tags, not
    latest
  • Prefer slim or alpine variants for smaller size
  • Scan base images for vulnerabilities
  • Consider distroless images for production
  • 使用特定版本标签,而非
    latest
  • 优先选择slim或alpine变体以减小体积
  • 扫描基础镜像以排查漏洞
  • 生产环境可考虑使用无发行版(distroless)镜像

Security Best Practices

安全最佳实践

  • Run containers as non-root user
  • Use read-only file systems where possible
  • Implement health checks
  • Scan images for vulnerabilities regularly
  • Use secrets management, not environment variables for sensitive data
  • Implement resource limits (CPU, memory)
  • 以非root用户运行容器
  • 尽可能使用只读文件系统
  • 配置健康检查
  • 定期扫描镜像以排查漏洞
  • 使用密钥管理工具存储敏感数据,而非环境变量
  • 设置资源限制(CPU、内存)

Docker Compose

Docker Compose

Configuration

配置

  • Use version 3+ compose files
  • Define networks explicitly
  • Use volumes for persistent data
  • Implement depends_on with health checks
  • Use environment files for configuration
  • 使用3.x版本及以上的Compose文件
  • 显式定义网络
  • 使用卷存储持久化数据
  • 结合健康检查配置depends_on
  • 使用环境文件管理配置

Development Workflow

开发工作流

  • Mount source code for hot reloading
  • Use override files for environment-specific config
  • Implement proper logging drivers
  • Use build args for build-time variables
  • 挂载源代码以实现热重载
  • 使用覆盖文件处理环境专属配置
  • 配置合适的日志驱动
  • 使用构建参数传递构建时变量

CI/CD Integration

CI/CD 集成

  • Build images in CI pipelines
  • Tag images with git commit SHA
  • Push to secure container registries
  • Implement automated vulnerability scanning
  • Use image signing for verification
  • 在CI流水线中构建镜像
  • 使用Git提交SHA为镜像打标签
  • 推送至安全的容器镜像仓库
  • 实施自动化漏洞扫描
  • 使用镜像签名进行验证

Networking

网络配置

  • Use user-defined bridge networks
  • Implement service discovery via DNS
  • Expose only necessary ports
  • Use network aliases for service communication
  • 使用用户自定义的桥接网络
  • 通过DNS实现服务发现
  • 仅暴露必要的端口
  • 使用网络别名实现服务间通信

Logging and Monitoring

日志与监控

  • Use appropriate logging drivers
  • Implement structured logging
  • Forward logs to centralized system
  • Monitor container metrics
  • Implement proper health checks
  • 使用合适的日志驱动
  • 实现结构化日志
  • 将日志转发至集中式系统
  • 监控容器指标
  • 配置完善的健康检查