docker-multi-stage

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Docker Multi-Stage Builds Skill

Docker 多阶段构建 Skill

Create optimized, minimal production images using multi-stage builds with language-specific patterns.
使用针对不同语言的模式,通过多阶段构建创建优化、轻量的生产镜像。

Purpose

目的

Reduce image size by 50-90% by separating build dependencies from runtime, following 2024-2025 best practices.
通过分离构建依赖与运行时环境,遵循2024-2025年最佳实践,将镜像大小减少50-90%。

Parameters

参数

ParameterTypeRequiredDefaultDescription
languageenumYes-node/python/go/rust/java
targetstringNoruntimeBuild target stage
base_runtimestringNo-Custom runtime base image
参数类型是否必填默认值描述
language枚举-node/python/go/rust/java
target字符串runtime构建目标阶段
base_runtime字符串-自定义运行时基础镜像

Multi-Stage Patterns

多阶段构建模式

Node.js (Alpine + Distroless)

Node.js(Alpine + Distroless)

dockerfile
undefined
dockerfile
undefined

Build stage

Build stage

FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build && npm prune --production
FROM node:20-alpine AS builder WORKDIR /app COPY package*.json ./ RUN npm ci COPY . . RUN npm run build && npm prune --production

Runtime stage (distroless = minimal attack surface)

Runtime stage (distroless = minimal attack surface)

FROM gcr.io/distroless/nodejs20-debian12 AS runtime WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules USER nonroot CMD ["dist/index.js"]
undefined
FROM gcr.io/distroless/nodejs20-debian12 AS runtime WORKDIR /app COPY --from=builder /app/dist ./dist COPY --from=builder /app/node_modules ./node_modules USER nonroot CMD ["dist/index.js"]
undefined

Python (Slim + Virtual Environment)

Python(Slim + 虚拟环境)

dockerfile
undefined
dockerfile
undefined

Build stage

Build stage

FROM python:3.12-slim AS builder WORKDIR /app RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt
FROM python:3.12-slim AS builder WORKDIR /app RUN python -m venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY requirements.txt . RUN pip install --no-cache-dir -r requirements.txt

Runtime stage

Runtime stage

FROM python:3.12-slim AS runtime WORKDIR /app COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY . . USER nobody CMD ["python", "main.py"]
undefined
FROM python:3.12-slim AS runtime WORKDIR /app COPY --from=builder /opt/venv /opt/venv ENV PATH="/opt/venv/bin:$PATH" COPY . . USER nobody CMD ["python", "main.py"]
undefined

Go (Scratch = Smallest Possible)

Go(Scratch = 最小体积)

dockerfile
undefined
dockerfile
undefined

Build stage

Build stage

FROM golang:1.22-alpine AS builder WORKDIR /app COPY go.* ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/server
FROM golang:1.22-alpine AS builder WORKDIR /app COPY go.* ./ RUN go mod download COPY . . RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /app/server

Runtime stage (scratch = 0 base size)

Runtime stage (scratch = 0 base size)

FROM scratch AS runtime COPY --from=builder /app/server /server COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ USER 65534 ENTRYPOINT ["/server"]
undefined
FROM scratch AS runtime COPY --from=builder /app/server /server COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ USER 65534 ENTRYPOINT ["/server"]
undefined

Rust (Musl for Static Linking)

Rust(Musl 静态链接)

dockerfile
undefined
dockerfile
undefined

Build stage

Build stage

FROM rust:1.75-alpine AS builder RUN apk add --no-cache musl-dev WORKDIR /app COPY . . RUN cargo build --release --target x86_64-unknown-linux-musl
FROM rust:1.75-alpine AS builder RUN apk add --no-cache musl-dev WORKDIR /app COPY . . RUN cargo build --release --target x86_64-unknown-linux-musl

Runtime stage

Runtime stage

FROM scratch AS runtime COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/app /app USER 65534 ENTRYPOINT ["/app"]
undefined
FROM scratch AS runtime COPY --from=builder /app/target/x86_64-unknown-linux-musl/release/app /app USER 65534 ENTRYPOINT ["/app"]
undefined

Java (JRE Only Runtime)

Java(仅JRE运行时)

dockerfile
undefined
dockerfile
undefined

Build stage

Build stage

FROM eclipse-temurin:21-jdk-alpine AS builder WORKDIR /app COPY . . RUN ./gradlew build --no-daemon
FROM eclipse-temurin:21-jdk-alpine AS builder WORKDIR /app COPY . . RUN ./gradlew build --no-daemon

Runtime stage (JRE only, not JDK)

Runtime stage (JRE only, not JDK)

FROM eclipse-temurin:21-jre-alpine AS runtime WORKDIR /app COPY --from=builder /app/build/libs/*.jar app.jar USER nobody ENTRYPOINT ["java", "-jar", "app.jar"]
undefined
FROM eclipse-temurin:21-jre-alpine AS runtime WORKDIR /app COPY --from=builder /app/build/libs/*.jar app.jar USER nobody ENTRYPOINT ["java", "-jar", "app.jar"]
undefined

Size Comparison

镜像大小对比

LanguageBeforeAfterReduction
Node.js1.2GB150MB87%
Python900MB120MB87%
Go800MB10MB99%
Rust1.5GB5MB99.7%
Java600MB200MB67%
语言优化前优化后缩减比例
Node.js1.2GB150MB87%
Python900MB120MB87%
Go800MB10MB99%
Rust1.5GB5MB99.7%
Java600MB200MB67%

Error Handling

错误处理

Common Errors

常见错误

ErrorCauseSolution
COPY --from failed
Stage not foundCheck stage name
not found
at runtime
Missing libsUse alpine, not scratch
permission denied
Non-root userCOPY --chown
错误原因解决方案
COPY --from failed
未找到指定阶段检查阶段名称
运行时提示
not found
缺少依赖库使用alpine镜像而非scratch
permission denied
非root用户权限问题使用COPY --chown参数

Fallback Strategy

回退策略

  1. Start with alpine instead of scratch/distroless
  2. Add required libraries incrementally
  3. Use
    ldd
    to identify missing dependencies
  1. 优先使用alpine镜像而非scratch/distroless
  2. 逐步添加所需依赖库
  3. 使用
    ldd
    命令识别缺失的依赖项

Troubleshooting

故障排查

Debug Checklist

调试检查清单

  • All required files copied to runtime stage?
  • SSL certificates included for HTTPS?
  • User/group exists in runtime image?
  • Build artifacts correctly located?
  • 是否所有必需文件都已复制到运行时阶段?
  • 是否包含了HTTPS所需的SSL证书?
  • 运行时镜像中是否存在指定的用户/用户组?
  • 构建产物是否位于正确路径?

Debug Commands

调试命令

bash
undefined
bash
undefined

Check final image size

Check final image size

docker images myapp:latest
docker images myapp:latest

Inspect layers

Inspect layers

docker history myapp:latest --no-trunc
docker history myapp:latest --no-trunc

Compare with baseline

Compare with baseline

dive myapp:latest
undefined
dive myapp:latest
undefined

Usage

使用方法

Skill("docker-multi-stage")
Skill("docker-multi-stage")

Assets

资源

  • assets/Dockerfile.node-multistage
    - Node.js template
  • assets/Dockerfile.python-multistage
    - Python template
  • assets/Dockerfile.node-multistage
    - Node.js 模板
  • assets/Dockerfile.python-multistage
    - Python 模板

Related Skills

相关技能

  • docker-optimization
  • dockerfile-basics
  • docker-optimization
  • dockerfile-basics