Skip to content
Skillv1.0.0

create-multistage-dockerfile

Create multi-stage Dockerfiles that separate build and runtime environments for minimal production images. Covers builder/runtime stage separation, artifact copying, scratch/distroless/alpine targets,

by pjt222(0) 0 installs
Free
Sign in to install

Free account. Installing gives you the manifest plus copy-paste snippets.

See reviews

About

Imported from pjt222/agent-almanac (i18n/wenyan-ultra/skills/create-multistage-dockerfile/SKILL.md). Install upstream with npx skills add pjt222/agent-almanac --skill create-multistage-dockerfile. Copyright stays with the author (MIT).

造多段 Dockerfile

構多段 Dockerfile 分構與行境以生最小產像。

  • 產像過大(>500MB 為編譯語)
  • 構具(編、發頭)入終像
  • 自一 Dockerfile 生發與產異像
  • 部於限境(邊、無服)

  • :存 Dockerfile 或待容器化案
  • :語與構系(npm、pip、go build、cargo、maven)
  • :目行基(slim、alpine、distroless、scratch)
  • :終像寸預

一:別構與行依

構段 行段
編譯 gcc、g++、rustc 不需
包理 npm、pip、cargo 或(解譯語)
發頭 -dev 不需
源碼 全源樹 僅編出
試架 jest、pytest 不需

二:構多段構

核模:構於肥像、複品至瘦像。

# ---- Build Stage ----
FROM <build-image> AS builder
WORKDIR /src
COPY <dependency-manifest> .
RUN <install-dependencies>
COPY . .
RUN <build-command>

# ---- Runtime Stage ----
FROM <runtime-image>
COPY --from=builder /src/<artifact> /<dest>
EXPOSE <port>
CMD [<entrypoint>]

三:施語專模

Node.js(剪 node_modules)

FROM node:22-bookworm AS builder
WORKDIR /src
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build && npm prune --omit=dev

FROM node:22-bookworm-slim
RUN groupadd -r app && useradd -r -g app app
WORKDIR /app
COPY --from=builder /src/dist ./dist
COPY --from=builder /src/node_modules ./node_modules
COPY --from=builder /src/package.json .
USER app
EXPOSE 3000
CMD ["node", "dist/index.js"]

Python(虛境複)

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

FROM python:3.12-slim-bookworm
COPY --from=builder /opt/venv /opt/venv
ENV PATH="/opt/venv/bin:$PATH"
WORKDIR /app
COPY --from=builder /src .
RUN groupadd -r app && useradd -r -g app app
USER app
EXPOSE 8000
CMD ["python", "app.py"]

Go(靜二入 scratch)

FROM golang:1.23-bookworm AS builder
WORKDIR /src
COPY go.mod go.sum ./
RUN go mod download
COPY . .
RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-s -w" -o /server ./cmd/server

FROM scratch
COPY --from=builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=builder /server /server
EXPOSE 8080
ENTRYPOINT ["/server"]

Rust(靜 musl 二)

FROM rust:1.82-bookworm AS builder
RUN apt-get update && apt-get install -y musl-tools && rm -rf /var/lib/apt/lists/*
RUN rustup target add x86_64-unknown-linux-musl
WORKDIR /src
COPY Cargo.toml Cargo.lock ./
RUN mkdir src && echo "fn main() {}" > src/main.rs \
    && cargo build --release --target x86_64-unknown-linux-musl \
    && rm -rf src
COPY . .
RUN touch src/main.rs && cargo build --release --target x86_64-unknown-linux-musl

FROM scratch
COPY --from=builder /src/target/x86_64-unknown-linux-musl/release/myapp /myapp
EXPOSE 8080
ENTRYPOINT ["/myapp"]

得: 終像僅含行與編品。

敗:COPY --from=builder 路。用 docker build --target builder 除構段。

四:擇行基

scratch 0 MB 靜 Go/Rust 二
gcr.io/distroless/static ~2 MB 靜二+CA 證
gcr.io/distroless/base ~20 MB 動二(libc)
*-slim 50-150 MB 解譯語
alpine ~7 MB 殼需時

注: Alpine 用 musl libc。某 Python wheels 與 Node native modules 或不行。解譯語宜用 -slim(glibc)。

五:跨段構引

ARG APP_VERSION=0.0.0

FROM golang:1.23 AS builder
ARG APP_VERSION
RUN go build -ldflags="-X main.version=${APP_VERSION}" -o /server .

FROM gcr.io/distroless/static
COPY --from=builder /server /server
ENTRYPOINT ["/server"]

構以:docker build --build-arg APP_VERSION=1.2.3 .

注: FROM 前之 ARG 為全。各段須重申 ARG 以用之。

六:較像寸

# Build both variants
docker build -t myapp:fat --target builder .
docker build -t myapp:slim .

# Compare sizes
docker images --format "table {{.Repository}}\t{{.Tag}}\t{{.Size}}" | grep myapp

得: 產像較構段小 50-90%。

  • docker build 諸段畢
  • 終像不含構具(編、發頭)
  • docker run 於瘦像正行
  • 像寸較單段顯減
  • COPY --from=builder 路正
  • 無源漏入產像

  • 缺行庫:編碼或需共庫(libclibssl)。瘦像須全試
  • COPY --from:品路須合。用 docker build --target builderdocker run --rm builder ls /path
  • Alpine musl 問:Native Node.js addons 與某 Python 包於 Alpine 敗。用 -slim
  • 全 ARG 範FROM 前申之 ARG 僅於 FROM 行可。各段用則重申
  • 忘 CA 證scratch 無證。自 builder 複 /etc/ssl/certs/ca-certificates.crt 或用 distroless

  • create-dockerfile - 單段標 Dockerfile
  • create-r-dockerfile - R 專 Dockerfile 用 rocker 像
  • optimize-docker-build-cache - 層快與 BuildKit 特
  • setup-compose-stack - 用多段像之 compose 設

Use it

Copy one of these into your project. Installing also returns the manifest and these snippets.

yaml
targets:
  - https://api.opensmartroute.ai/api/v1/registry/pjt222-agent-almanac-create-multistage-dockerfile/manifest   # or paste the manifest below

Manifest

An Open Capability Manifest: the router reads it to know what this does, what it costs and when to pick it.

pjt222-agent-almanac-create-multistage-dockerfile.ocm.jsonjson
{
  "ocm": "1",
  "id": "pjt222-agent-almanac-create-multistage-dockerfile",
  "kind": "skill",
  "name": "create-multistage-dockerfile",
  "description": "Create multi-stage Dockerfiles that separate build and runtime environments for minimal production images. Covers builder/runtime stage separation, artifact copying, scratch/distroless/alpine targets, and size comparison. Use when production images are too large, when build tools are included in the final image, when you need separate dev and prod images from one Dockerfile, or when deploying to constrained environments like edge or serverless.",
  "publisher": "pjt222",
  "version": "1.0.0",
  "capabilities": {
    "domains": [
      "general"
    ],
    "tags": [
      "skill-md",
      "docker",
      "multi-stage",
      "distroless",
      "alpine",
      "scratch",
      "optimization",
      "github"
    ],
    "languages": [
      "en"
    ]
  },
  "quality_prior": 0.6,
  "examples": [
    "Create multi-stage Dockerfiles that separate build and runtime environments for minimal production images. Covers builder/runtime stage separation, artifact copying, scratch/distroless/alpine targets, and size comparison. Use when production images are too large, when build tools are included in the final image, when you need separate dev and prod images from one Dockerfile, or when deploying to constrained environments like edge or serverless."
  ],
  "primary": false,
  "metadata": {
    "source": {
      "provider": "github",
      "repository": "https://github.com/pjt222/agent-almanac",
      "path": "i18n/wenyan-ultra/skills/create-multistage-dockerfile/SKILL.md",
      "ref": "cd78248a9de0516bf21c86923608a1c0eff017a4",
      "url": "https://github.com/pjt222/agent-almanac/blob/cd78248a9de0516bf21c86923608a1c0eff017a4/i18n/wenyan-ultra/skills/create-multistage-dockerfile/SKILL.md",
      "key": "pjt222/agent-almanac/i18n/wenyan-ultra/skills/create-multistage-dockerfile/SKILL.md"
    },
    "allowed_tools": [
      "Read",
      "Write",
      "Edit",
      "Bash",
      "Grep",
      "Glob"
    ],
    "license": "MIT"
  },
  "instructions": "# 造多段 Dockerfile\n\n構多段 Dockerfile 分構與行境以生最小產像。\n\n## 用\n\n- 產像過大(>500MB 為編譯語)\n- 構具(編、發頭)入終像\n- 自一 Dockerfile 生發與產異像\n- 部於限境(邊、無服)\n\n## 入\n\n- **必**:存 Dockerfile 或待容器化案\n- **必**:語與構系(npm、pip、go build、cargo、maven)\n- **可**:目行基(slim、alpine、distroless、scratch)\n- **可**:終像寸預\n\n## 行\n\n### 一:別構與行依\n\n| 類 | 構段 | 行段 |\n|---|---|---|\n| 編譯 | gcc、g++、rustc | 不需 |\n| 包理 | npm、pip、cargo | 或(解譯語) |\n| 發頭 | `-dev` 包 | 不需 |\n| 源碼 | 全源樹 | 僅編出 |\n| 試架 | jest、pytest | 不需 |\n\n### 二:構多段構\n\n核模:構於肥像、複品至瘦像。\n\n```dockerfile\n# ---- Build Stage ----\nFROM <build-image> AS builder\nWORKDIR /src\nCOPY <dependency-manifest> .\nRUN <install-dependencies",
  "cost": {
    "context_tokens": 1099
  }
}

Fetch it by URL: GET /api/v1/registry/pjt222-agent-almanac-create-multistage-dockerfile/manifest?version=1.0.0

Reviews

Star ratings from people who tried it. One review per account; edit yours any time.

No reviews yet. Install it, try it, and be the first to rate it.