by bossjones
Docker 容器化通过将应用程序及其依赖打包成可移植的容器来简化开发和部署。本技能提供从开发到生产的专业工作流,包括优化和调试工具。
1. 打开 Claude 聊天界面
2. 点击下方 "📋 复制" 按钮
3. 粘贴到 Claude 聊天框中并发送
4. 输入 "使用 docker-workflow 技能" 开始使用
=== docker-workflow 技能 === 作者: bossjones 描述: Docker 容器化通过将应用程序及其依赖打包成可移植的容器来简化开发和部署。本技能提供从开发到生产的专业工作流,包括优化和调试工具。 使用方法: 1. 调用技能: "使用 docker-workflow 技能" 2. 提供相关信息: 根据技能要求提供必要参数 3. 查看结果: 技能会返回处理结果 示例: "使用 docker-workflow 技能,帮我分析一下这段代码"
这种方法适用于所有 Claude 用户,不需要安装额外工具。
devops
safe
Comprehensive Docker containerization workflow covering multi-stage builds, docker-compose orchestration, image optimization, debugging, and production best practices.
Docker containerization streamlines development, testing, and deployment by packaging applications with their dependencies into portable, reproducible containers. This skill guides you through professional Docker workflows from development to production.
Use this skill when containerizing applications, setting up development environments, or deploying with Docker.
Ensure Docker is installed:
# macOS
brew install docker
# Ubuntu/Debian
sudo apt-get install docker.io docker-compose
# Verify installation
docker --version
docker-compose --version
Comprehensive guide covering Docker workflow phases from initial setup through production deployment, including multi-stage builds, docker-compose orchestration, optimization strategies, debugging tools, and deployment best practices.
docker_helper.sh - Utility script for common Docker operations:
Dockerfile.multi-stage - Templates for Node.js, Python, Go, Java, Rustdocker-compose.yml - Full-featured multi-service setup.dockerignore - Comprehensive ignore patterns# Stage 1: Build
FROM node:18-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
RUN npm run build
# Stage 2: Production
FROM node:18-alpine
WORKDIR /app
COPY --from=builder /app/dist ./dist
COPY --from=builder /app/node_modules ./node_modules
EXPOSE 3000
CMD ["node", "dist/index.js"]
version: '3.8'
services:
app:
build:
context: .
dockerfile: Dockerfile
ports:
- "3000:3000"
environment:
- DATABASE_URL=postgresql://db:5432/myapp
depends_on:
db:
condition: service_healthy
networks:
- app-network
db:
image: postgres:15-alpine
environment:
POSTGRES_DB: myapp
volumes:
- postgres-data:/var/lib/postgresql/data
healthcheck:
test: ["CMD-SHELL", "pg_isready -U user"]
interval: 5s
networks:
- app-network
volumes:
postgres-data:
networks:
app-network:
# Build image
docker build -t myapp:latest .
# Run with docker-compose
docker-compose up -d
# View logs
docker-compose logs -f app
# Stop
docker-compose down
Create .dockerignore:
node_modules/
__pycache__/
*.pyc
.git/
.env
*.log
dist/
build/
coverage/
Key principles:
Optimize Layer Caching:
# ✅ GOOD: Dependencies cached separately
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
# ❌ BAD: Any file change invalidates cache
COPY . .
RUN npm ci
Apply Security Best Practices:
# Use specific versions
FROM node:18.17.1-alpine
# Run as non-root user
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
USER nodejs
# Copy with ownership
COPY --chown=nodejs:nodejs . .
Use override files for different environments:
Development (docker-compose.override.yml):
services:
app:
build:
target: development
volumes:
- ./src:/app/src
environment:
- NODE_ENV=development
command: npm run dev
Production (docker-compose.prod.yml):
services:
app:
build:
target: production
restart: always
environment:
- NODE_ENV=production
Use the helper script:
# Check container health
./scripts/docker_helper.sh health myapp
# Inspect details
./scripts/docker_helper.sh inspect myapp
# View logs
./scripts/docker_helper.sh logs myapp 200
# Open shell
./scripts/docker_helper.sh shell myapp
# Analyze image size
./scripts/docker_helper.sh size myapp:latest
# Cleanup resources
./scripts/docker_helper.sh cleanup
Reduce Image Size:
Example:
# ✅ GOOD: Combined, cleaned up
RUN apt-get update && \
apt-get install -y --no-install-recommends package1 && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
Production Dockerfile:
FROM node:18-alpine AS production
# Security: non-root user
RUN addgroup -g 1001 -S nodejs && adduser -S nodejs -u 1001
WORKDIR /app
COPY --from=builder --chown=nodejs:nodejs /app/dist ./dist
USER nodejs
# Health check
HEALTHCHECK --interval=30s --timeout=3s \
CMD node healthcheck.js
EXPOSE 3000
CMD ["node", "dist/index.js"]
Deployment Commands:
# Tag for registry
docker tag myapp:latest registry.example.com/myapp:v1.0.0
# Push to registry
docker push registry.example.com/myapp:v1.0.0
# Deploy
docker-compose pull && docker-compose up -d
# Rolling update
docker-compose up -d --no-deps --build app
# Build
docker build -t myapp .
docker-compose build
# Run
docker run -d -p 3000:3000 myapp
docker-compose up -d
# Logs
docker logs -f myapp
docker-compose logs -f
# Execute
docker exec -it myapp sh
docker-compose exec app sh
# Stop
docker-compose down
# Clean
docker system prune -a
✅ Use specific image versions, not latest
✅ Run as non-root user
✅ Use secrets management for sensitive data
✅ Scan images for vulnerabilities
✅ Use minimal base images
✅ Use multi-stage builds ✅ Optimize layer caching ✅ Use .dockerignore ✅ Combine RUN commands ✅ Use BuildKit
✅ Use docker-compose for multi-container apps ✅ Use volumes for hot-reload ✅ Implement health checks ✅ Use proper dependency ordering
✅ Set restart policies ✅ Use orchestration (Swarm, Kubernetes) ✅ Monitor with health checks ✅ Use reverse proxy ✅ Implement rolling updates
Frontend + Backend + Database + Redis with docker-compose orchestration.
API Gateway + Multiple Services + Message Queue with network isolation.
Volume mounting for source code with dev-specific configuration.
Container exits immediately:
docker logs myapp
docker run -it --entrypoint sh myapp:latest
Network connectivity:
docker network inspect myapp_default
docker exec myapp ping db
Volume permissions:
# Fix in Dockerfile
RUN chown -R nodejs:nodejs /app/data
See SKILL.md for comprehensive documentation, detailed workflows, and advanced techniques.
See examples/ directory for complete Dockerfile templates and docker-compose configurations.
View Count
0
Download Count
0
Favorite Count
0
Quality Score
75