docker-containerization
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker Containerization Skill
Docker容器化技能
Overview
概述
Generate production-ready Docker configurations for modern web applications, particularly Next.js and Node.js projects. This skill provides Dockerfiles, docker-compose setups, bash scripts for container management, and comprehensive deployment guides for various orchestration platforms.
为现代Web应用(尤其是Next.js和Node.js项目)生成可用于生产环境的Docker配置。本技能提供Dockerfile、docker-compose部署配置、用于容器管理的bash脚本,以及针对各类编排平台的全面部署指南。
Core Capabilities
核心能力
1. Dockerfile Generation
1. Dockerfile生成
Create optimized Dockerfiles for different environments:
Production ():
assets/Dockerfile.production- Multi-stage build reducing image size by 85%
- Alpine Linux base (~180MB final image)
- Non-root user execution for security
- Health checks and resource limits
Development ():
assets/Dockerfile.development- Hot reload support
- All dev dependencies included
- Volume mounts for live code updates
Nginx Static ():
assets/Dockerfile.nginx- Static export optimization
- Nginx reverse proxy included
- Smallest possible footprint
针对不同环境创建优化后的Dockerfile:
生产环境():
assets/Dockerfile.production- 多阶段构建,镜像体积减少85%
- 基于Alpine Linux(最终镜像约180MB)
- 以非root用户运行,提升安全性
- 包含健康检查和资源限制
开发环境():
assets/Dockerfile.development- 支持热重载
- 包含所有开发依赖
- 卷挂载实现代码实时更新
Nginx静态部署():
assets/Dockerfile.nginx- 静态导出优化
- 内置Nginx反向代理
- 最小化镜像体积
2. Docker Compose Configuration
2. Docker Compose配置
Multi-container orchestration with :
assets/docker-compose.yml- Development and production services
- Network and volume management
- Health checks and logging
- Restart policies
通过实现多容器编排:
assets/docker-compose.yml- 开发和生产环境服务配置
- 网络与卷管理
- 健康检查与日志记录
- 重启策略
3. Bash Scripts for Container Management
3. 容器管理Bash脚本
docker-build.sh - Build images with comprehensive options:
bash
./docker-build.sh -e prod -t v1.0.0
./docker-build.sh -n my-app --no-cache --platform linux/amd64docker-run.sh - Run containers with full configuration:
bash
./docker-run.sh -i my-app -t v1.0.0 -d
./docker-run.sh -p 8080:3000 --env-file .env.productiondocker-push.sh - Push to registries (Docker Hub, ECR, GCR, ACR):
bash
./docker-push.sh -n my-app -t v1.0.0 --repo username/my-app
./docker-push.sh -r gcr.io/project --repo my-app --also-tag stabledocker-cleanup.sh - Free disk space:
bash
./docker-cleanup.sh --all --dry-run # Preview cleanup
./docker-cleanup.sh --containers --images # Clean specific resourcesdocker-build.sh - 提供丰富选项的镜像构建脚本:
bash
./docker-build.sh -e prod -t v1.0.0
./docker-build.sh -n my-app --no-cache --platform linux/amd64docker-run.sh - 全配置化的容器运行脚本:
bash
./docker-run.sh -i my-app -t v1.0.0 -d
./docker-run.sh -p 8080:3000 --env-file .env.productiondocker-push.sh - 推送镜像至镜像仓库(Docker Hub、ECR、GCR、ACR):
bash
./docker-push.sh -n my-app -t v1.0.0 --repo username/my-app
./docker-push.sh -r gcr.io/project --repo my-app --also-tag stabledocker-cleanup.sh - 释放磁盘空间:
bash
./docker-cleanup.sh --all --dry-run # 预览清理内容
./docker-cleanup.sh --containers --images # 清理指定资源4. Configuration Files
4. 配置文件
- : Excludes unnecessary files (node_modules, .git, logs)
.dockerignore - : Production-ready Nginx configuration with compression, caching, security headers
nginx.conf
- :排除不必要的文件(node_modules、.git、日志等)
.dockerignore - :可用于生产环境的Nginx配置,包含压缩、缓存、安全头设置
nginx.conf
5. Reference Documentation
5. 参考文档
docker-best-practices.md covers:
- Multi-stage builds explained
- Image optimization techniques (50-85% size reduction)
- Security best practices (non-root users, vulnerability scanning)
- Performance optimization
- Health checks and logging
- Troubleshooting guide
container-orchestration.md covers deployment to:
- Docker Compose (local development)
- Kubernetes (enterprise scale with auto-scaling)
- Amazon ECS (AWS-native orchestration)
- Google Cloud Run (serverless containers)
- Azure Container Instances
- Digital Ocean App Platform
Includes configuration examples, commands, auto-scaling setup, and monitoring.
docker-best-practices.md 涵盖:
- 多阶段构建详解
- 镜像优化技巧(体积减少50-85%)
- 安全最佳实践(非root用户、漏洞扫描)
- 性能优化
- 健康检查与日志记录
- 故障排除指南
container-orchestration.md 涵盖部署至以下平台的内容:
- Docker Compose(本地开发)
- Kubernetes(企业级规模,支持自动扩缩容)
- Amazon ECS(AWS原生编排)
- Google Cloud Run(无服务器容器)
- Azure容器实例
- Digital Ocean应用平台
包含配置示例、命令、自动扩缩容设置和监控方案。
Workflow Decision Tree
工作流决策树
1. What environment?
1. 选择环境?
- Development → (hot reload, all dependencies)
Dockerfile.development - Production → (minimal, secure, optimized)
Dockerfile.production - Static Export → (smallest footprint)
Dockerfile.nginx
- 开发环境 → 使用(热重载、包含所有依赖)
Dockerfile.development - 生产环境 → 使用(轻量、安全、优化)
Dockerfile.production - 静态导出 → 使用(最小镜像体积)
Dockerfile.nginx
2. Single or Multi-container?
2. 单容器还是多容器?
- Single → Generate Dockerfile only
- Multi → Generate (app + database, microservices)
docker-compose.yml
- 单容器 → 仅生成Dockerfile
- 多容器 → 生成(应用+数据库、微服务)
docker-compose.yml
3. Which registry?
3. 选择哪个镜像仓库?
- Docker Hub →
docker.io/username/image - AWS ECR →
123456789012.dkr.ecr.region.amazonaws.com/image - Google GCR →
gcr.io/project-id/image - Azure ACR →
registry.azurecr.io/image
- Docker Hub →
docker.io/username/image - AWS ECR →
123456789012.dkr.ecr.region.amazonaws.com/image - Google GCR →
gcr.io/project-id/image - Azure ACR →
registry.azurecr.io/image
4. Deployment platform?
4. 选择哪个部署平台?
- Kubernetes → See K8s section
references/container-orchestration.md - ECS → See ECS task definition examples
- Cloud Run → See deployment commands
- Docker Compose → Use provided compose file
- Kubernetes → 查看中的K8s章节
references/container-orchestration.md - ECS → 查看ECS任务定义示例
- Cloud Run → 查看部署命令
- Docker Compose → 使用提供的compose文件
5. Optimizations needed?
5. 需要哪些优化?
- Image size → Multi-stage builds, Alpine base
- Build speed → Layer caching, BuildKit
- Security → Non-root user, vulnerability scanning
- Performance → Resource limits, health checks
- 镜像体积 → 多阶段构建、Alpine基础镜像
- 构建速度 → 层缓存、BuildKit
- 安全性 → 非root用户、漏洞扫描
- 性能 → 资源限制、健康检查
Usage Examples
使用示例
Example 1: Containerize Next.js App for Production
示例1:将Next.js应用容器化用于生产环境
User: "Containerize my Next.js app for production"
Steps:
- Copy to project root as
assets/Dockerfile.productionDockerfile - Copy to project root
assets/.dockerignore - Build:
./docker-build.sh -e prod -n my-app -t v1.0.0 - Test:
./docker-run.sh -i my-app -t v1.0.0 -p 3000:3000 -d - Push:
./docker-push.sh -n my-app -t v1.0.0 --repo username/my-app
用户需求:"将我的Next.js应用容器化用于生产环境"
步骤:
- 将复制到项目根目录并命名为
assets/Dockerfile.productionDockerfile - 将复制到项目根目录
assets/.dockerignore - 构建镜像:
./docker-build.sh -e prod -n my-app -t v1.0.0 - 测试运行:
./docker-run.sh -i my-app -t v1.0.0 -p 3000:3000 -d - 推送镜像:
./docker-push.sh -n my-app -t v1.0.0 --repo username/my-app
Example 2: Development with Docker Compose
示例2:使用Docker Compose搭建本地开发环境
User: "Set up Docker Compose for local development"
Steps:
- Copy and
assets/Dockerfile.developmentto projectassets/docker-compose.yml - Customize services in docker-compose.yml
- Start:
docker-compose up -d - Logs:
docker-compose logs -f app-dev
用户需求:"为本地开发设置Docker Compose"
步骤:
- 将和
assets/Dockerfile.development复制到项目中assets/docker-compose.yml - 自定义docker-compose.yml中的服务配置
- 启动服务:
docker-compose up -d - 查看日志:
docker-compose logs -f app-dev
Example 3: Deploy to Kubernetes
示例3:部署到Kubernetes
User: "Deploy my containerized app to Kubernetes"
Steps:
- Build and push image to registry
- Review Kubernetes section
references/container-orchestration.md - Create K8s manifests (deployment, service, ingress)
- Apply:
kubectl apply -f deployment.yaml - Verify:
kubectl get pods && kubectl logs -f deployment/app
用户需求:"将我的容器化应用部署到Kubernetes"
步骤:
- 构建并推送镜像至镜像仓库
- 查看中的Kubernetes章节
references/container-orchestration.md - 创建K8s清单文件(deployment、service、ingress)
- 应用配置:
kubectl apply -f deployment.yaml - 验证部署:
kubectl get pods && kubectl logs -f deployment/app
Example 4: Deploy to AWS ECS
示例4:部署到AWS ECS
User: "Deploy to AWS ECS Fargate"
Steps:
- Build and push to ECR
- Review ECS section
references/container-orchestration.md - Create task definition JSON
- Register:
aws ecs register-task-definition --cli-input-json file://task-def.json - Create service:
aws ecs create-service --cluster my-cluster --service-name app --desired-count 3
用户需求:"部署到AWS ECS Fargate"
步骤:
- 构建并推送镜像至ECR
- 查看中的ECS章节
references/container-orchestration.md - 创建任务定义JSON文件
- 注册任务定义:
aws ecs register-task-definition --cli-input-json file://task-def.json - 创建服务:
aws ecs create-service --cluster my-cluster --service-name app --desired-count 3
Best Practices
最佳实践
Security
安全性
✅ Use multi-stage builds for production
✅ Run as non-root user
✅ Use specific image tags (not )
✅ Scan for vulnerabilities
✅ Never hardcode secrets
✅ Implement health checks
latest✅ 生产环境使用多阶段构建
✅ 以非root用户运行容器
✅ 使用特定镜像标签(而非)
✅ 扫描镜像漏洞
✅ 绝不硬编码密钥
✅ 实现健康检查
latestPerformance
性能
✅ Optimize layer caching order
✅ Use Alpine images (~85% smaller)
✅ Enable BuildKit for parallel builds
✅ Set resource limits
✅ Use compression
✅ 优化层缓存顺序
✅ 使用Alpine镜像(体积减少约85%)
✅ 启用BuildKit实现并行构建
✅ 设置资源限制
✅ 启用压缩
Maintainability
可维护性
✅ Add comments for complex steps
✅ Use build arguments for flexibility
✅ Keep Dockerfiles DRY
✅ Version control all configs
✅ Document environment variables
✅ 为复杂步骤添加注释
✅ 使用构建参数提升灵活性
✅ 保持Dockerfile简洁(DRY原则)
✅ 对所有配置文件进行版本控制
✅ 记录环境变量
Troubleshooting
故障排除
Image too large (>500MB)
→ Use multi-stage builds, Alpine base, comprehensive .dockerignore
Build is slow
→ Optimize layer caching, use BuildKit, review dependencies
Container exits immediately
→ Check logs:
→ Verify CMD/ENTRYPOINT, check port conflicts
docker logs container-nameChanges not reflecting
→ Rebuild without cache, check .dockerignore, verify volume mounts
镜像体积过大(>500MB)
→ 使用多阶段构建、Alpine基础镜像、完善的.dockerignore文件
构建速度慢
→ 优化层缓存、使用BuildKit、检查依赖项
容器立即退出
→ 查看日志:
→ 验证CMD/ENTRYPOINT配置、检查端口冲突
docker logs container-name代码变更未同步到容器
→ 不使用缓存重新构建、检查.dockerignore文件、验证卷挂载配置
Quick Reference
快速参考
bash
undefinedbash
undefinedBuild
构建镜像
./docker-build.sh -e prod -t latest
./docker-build.sh -e prod -t latest
Run
运行容器
./docker-run.sh -i app -t latest -d
./docker-run.sh -i app -t latest -d
Logs
查看日志
docker logs -f app
docker logs -f app
Execute
进入容器执行命令
docker exec -it app sh
docker exec -it app sh
Cleanup
清理资源
./docker-cleanup.sh --all --dry-run # Preview
./docker-cleanup.sh --all # Execute
undefined./docker-cleanup.sh --all --dry-run # 预览清理内容
./docker-cleanup.sh --all # 执行清理
undefinedIntegration with CI/CD
与CI/CD集成
GitHub Actions
GitHub Actions
yaml
- run: |
chmod +x docker-build.sh docker-push.sh
./docker-build.sh -e prod -t ${{ github.sha }}
./docker-push.sh -n app -t ${{ github.sha }} --repo username/appyaml
- run: |
chmod +x docker-build.sh docker-push.sh
./docker-build.sh -e prod -t ${{ github.sha }}
./docker-push.sh -n app -t ${{ github.sha }} --repo username/appGitLab CI
GitLab CI
yaml
build:
script:
- chmod +x docker-build.sh
- ./docker-build.sh -e prod -t $CI_COMMIT_SHAyaml
build:
script:
- chmod +x docker-build.sh
- ./docker-build.sh -e prod -t $CI_COMMIT_SHAResources
资源
Scripts (scripts/
)
scripts/脚本(scripts/
)
scripts/Production-ready bash scripts with comprehensive features:
- - Build images (400+ lines, colorized output)
docker-build.sh - - Run containers (400+ lines, auto conflict resolution)
docker-run.sh - - Push to registries (multi-registry support)
docker-push.sh - - Clean resources (dry-run mode, selective cleanup)
docker-cleanup.sh
可用于生产环境的bash脚本,功能全面:
- - 镜像构建脚本(400+行,带彩色输出)
docker-build.sh - - 容器运行脚本(400+行,自动解决冲突)
docker-run.sh - - 镜像推送脚本(支持多仓库)
docker-push.sh - - 资源清理脚本(支持预览模式、选择性清理)
docker-cleanup.sh
References (references/
)
references/参考文档(references/
)
references/Detailed documentation loaded as needed:
- - Comprehensive Docker best practices (~500 lines)
docker-best-practices.md - - Deployment guides for 6+ platforms (~600 lines)
container-orchestration.md
按需加载的详细文档:
- - 全面的Docker最佳实践(约500行)
docker-best-practices.md - - 6+平台的部署指南(约600行)
container-orchestration.md
Assets (assets/
)
assets/资源文件(assets/
)
assets/Ready-to-use templates:
- - Multi-stage production Dockerfile
Dockerfile.production - - Development Dockerfile
Dockerfile.development - - Static export with Nginx
Dockerfile.nginx - - Multi-container orchestration
docker-compose.yml - - Optimized exclusion rules
.dockerignore - - Production Nginx configuration
nginx.conf
可直接使用的模板:
- - 多阶段生产环境Dockerfile
Dockerfile.production - - 开发环境Dockerfile
Dockerfile.development - - 带Nginx的静态导出Dockerfile
Dockerfile.nginx - - 多容器编排配置
docker-compose.yml - - 优化后的文件排除规则
.dockerignore - - 生产环境Nginx配置
nginx.conf