Files
docker_practice/09_buildx/9.1_buildkit.md
Baohua Yang b44c9acd6c Restruct
2026-02-09 12:56:12 -08:00

169 lines
6.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## 9.1 使用 `BuildKit` 构建镜像
**BuildKit** 是下一代的镜像构建组件 https://github.com/moby/buildkit 开源。
> **重要** Docker 23.0 BuildKit 已成为**默认稳定构建器**无需手动启用Docker Engine v29 进一步将 Containerd 镜像存储设为默认提升与 Kubernetes 的互操作性
目前Docker Hub 自动构建已经支持 BuildKit具体请参考 https://github.com/docker-practice/docker-hub-buildx
### `Dockerfile` 新增指令详解
BuildKit 引入了多项新指令旨在优化构建缓存和安全性以下将详细介绍这些指令的用法
使用 BuildKit 我们可以使用下面几个新的 `Dockerfile` 指令来加快镜像构建
要使用最新的 Dockerfile 语法特性建议在 Dockerfile 开头添加语法指令
```docker
## syntax=docker/dockerfile:1
```
这将使用最新的稳定版语法解析器确保你可以使用所有最新特性
#### `RUN --mount=type=cache`
目前几乎所有的程序都会使用依赖管理工具例如 `Go` 中的 `go mod``Node.js` 中的 `npm` 等等当我们构建一个镜像时往往会重复的从互联网中获取依赖包难以缓存大大降低了镜像的构建效率
例如一个前端工程需要用到 `npm`
```docker
FROM node:alpine as builder
WORKDIR /app
COPY package.json /app/
RUN npm i --registry=https://registry.npmmirror.com \
&& rm -rf ~/.npm
COPY src /app/src
RUN npm run build
FROM nginx:alpine
COPY --from=builder /app/dist /app/dist
```
使用多阶段构建构建的镜像中只包含了目标文件夹 `dist`但仍然存在一些问题 `package.json` 文件变动时`RUN npm i && rm -rf ~/.npm` 这一层会重新执行变更多次后生成了大量的中间层镜像
为解决这个问题进一步的我们可以设想一个类似 **数据卷** 的功能在镜像构建时把 `node_modules` 文件夹挂载上去在构建完成后这个 `node_modules` 文件夹会自动卸载实际的镜像中并不包含 `node_modules` 这个文件夹这样我们就省去了每次获取依赖的时间大大增加了镜像构建效率同时也避免了生成了大量的中间层镜像
`BuildKit` 提供了 `RUN --mount=type=cache` 指令可以实现上边的设想
```docker
## syntax=docker/dockerfile:1
FROM node:alpine as builder
WORKDIR /app
COPY package.json /app/
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
--mount=type=cache,target=/root/.npm,id=npm_cache \
npm i --registry=https://registry.npmmirror.com
COPY src /app/src
RUN --mount=type=cache,target=/app/node_modules,id=my_app_npm_module,sharing=locked \
## --mount=type=cache,target=/app/dist,id=my_app_dist,sharing=locked \
npm run build
FROM nginx:alpine
## COPY --from=builder /app/dist /app/dist
## 为了更直观的说明 from 和 source 指令,这里使用 RUN 指令
RUN --mount=type=cache,target=/tmp/dist,from=builder,source=/app/dist \
# --mount=type=cache,target/tmp/dist,from=my_app_dist,sharing=locked \
mkdir -p /app/dist && cp -r /tmp/dist/* /app/dist
```
第一个 `RUN` 指令执行后`id` `my_app_npm_module` 的缓存文件夹挂载到了 `/app/node_modules` 文件夹中多次执行也不会产生多个中间层镜像
第二个 `RUN` 指令执行时需要用到 `node_modules` 文件夹`node_modules` 已经挂载命令也可以正确执行
第三个 `RUN` 指令将上一阶段产生的文件复制到指定位置`from` 指明缓存的来源这里 `builder` 表示缓存来源于构建的第一阶段`source` 指明缓存来源的文件夹
上面的 `Dockerfile` `--mount=type=cache,...` 中指令作用如下
|Option |Description|
|---------------------|-----------|
|`id` | `id` 设置一个标志以便区分缓存|
|`target` (必填项) | 缓存的挂载目标文件夹|
|`ro`,`readonly` | 只读缓存文件夹不能被写入 |
|`sharing` | `shared` `private` `locked` 值可供选择`sharing` 设置当一个缓存被多次使用时的表现由于 `BuildKit` 支持并行构建当多个步骤使用同一缓存时同一 `id`会发生冲突`shared` 表示多个步骤可以同时读写`private` 表示当多个步骤使用同一缓存时每个步骤使用不同的缓存`locked` 表示当一个步骤完成释放缓存后后一个步骤才能继续使用该缓存|
|`from` | 缓存来源构建阶段不填写时为空文件夹|
|`source` | 来源的文件夹路径|
#### `RUN --mount=type=bind`
该指令可以将一个镜像或上一构建阶段的文件挂载到指定位置
```docker
## syntax=docker/dockerfile:1
RUN --mount=type=bind,from=php:alpine,source=/usr/local/bin/docker-php-entrypoint,target=/docker-php-entrypoint \
cat /docker-php-entrypoint
```
#### `RUN --mount=type=tmpfs`
该指令可以将一个 `tmpfs` 文件系统挂载到指定位置
```docker
## syntax=docker/dockerfile:1
RUN --mount=type=tmpfs,target=/temp \
mount | grep /temp
```
#### `RUN --mount=type=secret`
该指令可以将一个文件(例如密钥)挂载到指定位置
```docker
## syntax=docker/dockerfile:1
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
cat /root/.aws/credentials
```
```bash
$ docker build -t test --secret id=aws,src=$HOME/.aws/credentials .
```
#### `RUN --mount=type=ssh`
该指令可以挂载 `ssh` 密钥
```docker
## syntax=docker/dockerfile:1
FROM alpine
RUN apk add --no-cache openssh-client
RUN mkdir -p -m 0700 ~/.ssh && ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
RUN --mount=type=ssh ssh git@gitlab.com | tee /hello
```
```bash
$ eval $(ssh-agent)
$ ssh-add ~/.ssh/id_rsa
(Input your passphrase here)
$ docker build -t test --ssh default=$SSH_AUTH_SOCK .
```
### docker compose build 使用 BuildKit
Docker Compose 同样支持 BuildKit这使得多服务应用的构建更加高效
Docker 23.0 BuildKit 已默认启用无需额外配置如果使用旧版本可设置 `DOCKER_BUILDKIT=1` 环境变量启用
### 官方文档
* https://github.com/moby/buildkit/blob/master/frontend/dockerfile/docs/experimental.md