with one click
docker-best-practices
// Docker 多服务部署最佳实践 - 遵循生产级 Dockerfile 和 Docker Compose 架构原则。核心原则:Dockerfile 不随环境变化、compose 不包含真实 secret、所有环境差异通过 .env 管理、容器可销毁但数据不可、恢复能力优于自动化炫技。涵盖:Dockerfile 优化、多服务 compose 架构、健康检查、数据持久化、安全配置。
// Docker 多服务部署最佳实践 - 遵循生产级 Dockerfile 和 Docker Compose 架构原则。核心原则:Dockerfile 不随环境变化、compose 不包含真实 secret、所有环境差异通过 .env 管理、容器可销毁但数据不可、恢复能力优于自动化炫技。涵盖:Dockerfile 优化、多服务 compose 架构、健康检查、数据持久化、安全配置。
[HINT] Download the complete skill directory including SKILL.md and all related files
| name | docker-best-practices |
| description | Docker 多服务部署最佳实践 - 遵循生产级 Dockerfile 和 Docker Compose 架构原则。核心原则:Dockerfile 不随环境变化、compose 不包含真实 secret、所有环境差异通过 .env 管理、容器可销毁但数据不可、恢复能力优于自动化炫技。涵盖:Dockerfile 优化、多服务 compose 架构、健康检查、数据持久化、安全配置。 |
Docker 多服务部署最佳实践 - 生产级 Dockerfile 和 Docker Compose 架构指南。
This skill should be triggered when:
必须遵守的 5 条原则:
Dockerfile 不随环境变化
.env 文件注入Compose 不包含真实 secret
compose.yaml 只包含 secret 的占位符或引用.env、secret manager 或挂载文件读取compose.yaml 包含的 secret 提交到版本控制所有环境差异 → .env
.env.{environment} 文件.env.example 提供模板,但不包含真实值容器可销毁,数据不可
恢复能力 > 自动化炫技
必须遵循的 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"]
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
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
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
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
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
This skill includes comprehensive documentation in references/:
getting_started.md (4 pages, confidence: medium)
.dockerignore usagedockerfile.md (2 pages, confidence: medium)
compose.md (5 pages, confidence: medium)
security.md (1 page, confidence: medium)
other.md (1 page, confidence: medium)
Use these reference files when you need:
Building a production image:
Deploying with Compose:
compose.yaml with service definitions.env for environment-specific valuescompose.production.yaml) for different environmentsManaging data:
To refresh this skill with updated documentation: