0Pricing
Docker & Kubernetes for Developers · 课时

使用多阶段构建创建精简生产镜像

掌握多阶段 Dockerfile,在构建阶段编译或打包应用,然后只将构建产物复制到体积很小的运行时镜像中

使用多阶段构建创建精简生产镜像 是 CoddyKit 上的免费 Docker & Kubernetes for Developers 课时。 这是第 4 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Docker & Kubernetes for Developers 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Docker & Kubernetes for Developers 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

The Fat Image Problem

A single-stage build often ships compilers, dev dependencies, and source code into production. The result is a large, slower-to-pull, larger-attack-surface image.

What Is a Multi-Stage Build

A multi-stage build uses several FROM statements in one Dockerfile. Earlier stages build artifacts; a final lean stage copies only what it needs to run.

A Go Multi-Stage Example

Compile in a Go image, then ship a tiny final image with just the binary.

FROM golang:1.22 AS build
WORKDIR /src
COPY . .
RUN go build -o app .

FROM alpine:3.20
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]

Copying From a Named Stage

The key line is COPY --from=build. It pulls the compiled app binary out of the build stage, leaving the Go toolchain behind.

A Node.js Multi-Stage Example

Install and build with dev deps, then copy only the production output.

FROM node:20 AS build
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:20-slim
WORKDIR /app
COPY --from=build /app/dist ./dist
COPY package*.json ./
RUN npm ci --omit=dev
CMD ["node", "dist/index.js"]

Choosing a Slim Base

Final stages should use minimal bases like alpine, -slim, or distroless. Smaller bases mean fewer packages and fewer vulnerabilities.

Distroless Images

Google distroless images contain only your app and its runtime, no shell or package manager. They are very small and hard to exploit interactively.

FROM gcr.io/distroless/static-debian12
COPY --from=build /src/app /app
ENTRYPOINT ["/app"]

Targeting a Specific Stage

You can build only up to a named stage with --target, useful for running tests in CI.

docker build --target build -t myapp:test .

Caching Dependencies

Copy dependency manifests before the rest of the source. Then dependency installation is cached and only re-runs when the manifest changes.

COPY package*.json ./
RUN npm ci
COPY . .

Measuring the Win

Compare sizes before and after. Multi-stage builds commonly shrink images from hundreds of MB to tens of MB.

docker images myapp

Combining With .dockerignore

Add a .dockerignore so build context excludes node_modules, .git, and secrets, speeding builds and avoiding leaking files into images.

node_modules
.git
*.env

Quick Check

Test what you have learned.

Recap

You learned to split Dockerfiles into build and runtime stages, copy only artifacts with --from, pick slim or distroless bases, target stages, and combine with .dockerignore for lean production images.

常见问题解答

「使用多阶段构建创建精简生产镜像」课时是免费的吗?

是的 — 「使用多阶段构建创建精简生产镜像」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Docker & Kubernetes for Developers 课程的其余内容,请升级到 CoddyKit PRO。 Docker & Kubernetes for Developers 课程共包含 4 节课。

「使用多阶段构建创建精简生产镜像」这节课中我会学到什么?

掌握多阶段 Dockerfile,在构建阶段编译或打包应用,然后只将构建产物复制到体积很小的运行时镜像中 你通过在浏览器中直接运行的动手代码来练习 Docker & Kubernetes for Developers,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Docker & Kubernetes for Developers 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Docker & Kubernetes for Developers 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 4 节课,共 4 节。

「使用多阶段构建创建精简生产镜像」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Docker & Kubernetes for Developers 课中编写并运行代码吗?

能。每节 Docker & Kubernetes for Developers 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 将 Web 应用容器化
  2. 优化 Docker 镜像
  3. 安全与生产环境最佳实践
  4. 使用多阶段构建创建精简生产镜像
← 返回 Docker & Kubernetes for Developers