docker-development
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDocker Development
Docker 开发
Patterns for building, testing, and deploying Docker containers.
构建、测试和部署Docker容器的模式。
Core Principles
核心原则
- Minimal images -- Alpine/distroless, multi-stage builds
- Security first -- Non-root USER, no secrets in layers, pin versions
- Testable -- Verifiable in CI with entrypoint bypass and DNS mocking
- Cache-efficient -- Copy dependency files first, clean in same layer
- 最小化镜像 -- 使用Alpine/distroless基础镜像、多阶段构建
- 安全优先 -- 使用非root用户USER、不在镜像层中存储密钥、固定版本
- 可测试性 -- 在CI中通过绕过入口点和DNS模拟进行验证
- 缓存高效 -- 先复制依赖文件,在同一层中清理无用内容
Quick Reference
快速参考
Multi-Stage Build (Node.js)
多阶段构建(Node.js)
dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
FROM node:20-alpine
RUN addgroup -g 1001 app && adduser -u 1001 -G app -D app
USER app
COPY /app .
HEALTHCHECK \
CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "server.js"]dockerfile
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci --only=production
COPY . .
FROM node:20-alpine
RUN addgroup -g 1001 app && adduser -u 1001 -G app -D app
USER app
COPY /app .
HEALTHCHECK \
CMD wget -qO- http://localhost:3000/health || exit 1
CMD ["node", "server.js"]Multi-Stage Build (Go -- scratch/distroless)
多阶段构建(Go -- scratch/distroless)
dockerfile
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app/server .
FROM gcr.io/distroless/static:nonroot
COPY /app/server /server
CMD ["/server"]dockerfile
FROM golang:1.22-alpine AS builder
WORKDIR /app
COPY go.* ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 go build -o /app/server .
FROM gcr.io/distroless/static:nonroot
COPY /app/server /server
CMD ["/server"]Layer Optimization
镜像层优化
dockerfile
RUN apt-get update && \
apt-get install -y --no-install-recommends curl && \
rm -rf /var/lib/apt/lists/*dockerfile
RUN apt-get update && \
apt-get install -y --no-install-recommends curl && \
rm -rf /var/lib/apt/lists/*Build Cache: Copy Dependency Files First
构建缓存:先复制依赖文件
dockerfile
COPY package*.json ./
RUN npm ci
COPY . .Dependency manifests before source so install layers stay cached on source-only changes.
dockerfile
COPY package*.json ./
RUN npm ci
COPY . .先复制依赖清单再复制源码,这样当仅源码变化时,安装依赖的镜像层会保持缓存。
BuildKit Secrets
BuildKit 密钥管理
dockerfile
RUN git clone git@github.com:org/repo.gitSecrets in // persist in layer history (). Use .
ENVARGCOPYdocker history--mount=type=secretdockerfile
RUN git clone git@github.com:org/repo.git通过//设置的密钥会保留在镜像层历史中(可通过查看)。请使用方式。
ENVARGCOPYdocker history--mount=type=secretDocker Bake (Multi-Platform)
Docker Bake(多平台构建)
hcl
target "app" {
platforms = ["linux/amd64", "linux/arm64"]
cache-from = ["type=gha"]
cache-to = ["type=gha,mode=max"]
}hcl
target "app" {
platforms = ["linux/amd64", "linux/arm64"]
cache-from = ["type=gha"]
cache-to = ["type=gha,mode=max"]
}Security Anti-Patterns
安全反模式
| Anti-pattern | Fix |
|---|---|
| Pin version: |
No | |
| Use specific permissions: |
| Remove or use specific |
| Mount only needed paths |
| Bind to |
| Use |
| 反模式 | 修复方案 |
|---|---|
| 固定版本: |
未设置 | 使用 |
| 使用特定权限: |
compose中设置 | 移除该配置或使用特定的 |
| 仅挂载所需路径 |
| 绑定到 |
| 使用 |
CI Testing Gotchas
CI测试注意事项
- Bypass entrypoint:
docker run --rm --entrypoint php myimage -v - Mock upstream DNS:
docker run --rm --add-host backend:127.0.0.1 nginx-image nginx -t - Compose validation: before
cp .env.example .envdocker compose config - Secret scanning: Exclude , README, docs from scanners
.env.example
- 绕过入口点:
docker run --rm --entrypoint php myimage -v - 模拟上游DNS:
docker run --rm --add-host backend:127.0.0.1 nginx-image nginx -t - Compose验证:在执行前复制
docker compose config为.env.example.env - 密钥扫描:将、README、文档排除在扫描范围外
.env.example
.dockerignore
.dockerignore配置
Exclude: , /, , ,
.gitnode_modulesvendor.env**.pem*.key需排除的内容:、/、、、
.gitnode_modulesvendor.env**.pem*.keyCompose Essentials
Compose核心要点
- with
depends_on+condition: service_healthywithhealthcheckfor startup orderingstart_period - with
networksfor database isolation from external accessinternal: true - for optional services that only start with
profiles: [debug]--profile debug
- 使用搭配
depends_on,并为依赖服务设置包含condition: service_healthy的start_period来控制启动顺序healthcheck - 使用并设置
networks实现数据库与外部访问隔离internal: true - 使用定义可选服务,仅在添加
profiles: [debug]参数时启动--profile debug
References
参考资料
- -- Comprehensive CI testing patterns for Docker images
references/ci-testing.md
- -- Docker镜像的CI测试完整模式指南
references/ci-testing.md