Files
docker_practice/10_buildx/10.2_buildx.md
2026-03-09 20:04:13 -07:00

53 lines
1.8 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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.

## 10.2 使用 buildx 构建镜像
### 10.2.1 使用
Buildx 的使用非常直观绝大多数情况下可以替代 `docker build` 命令
你可以直接使用 `docker buildx build` 命令构建镜像
```bash
$ docker buildx build .
[+] Building 8.4s (23/32)
=> ...
```
Buildx 使用 [BuildKit 引擎](10.1_buildkit.md)进行构建支持许多新的功能具体参考 [Buildkit](10.1_buildkit.md) 一节
#### 使用 `bake`
`docker buildx bake` 是一个高级构建命令支持从 HCLJSON Compose 文件中定义构建目标实现复杂的流水线构建
```bash
## 从 Compose 文件构建所有服务
$ docker buildx bake
## 仅构建指定目标
$ docker buildx bake web
```
#### 生成 SBOM
Buildx 支持在构建时直接生成 SBOM (Software Bill of Materials)这对于软件供应链安全至关重要
```bash
$ docker buildx build --sbom=true -t myimage .
```
该命令会在构建结果中包含 SPDX CycloneDX 格式的 SBOM 数据
> ** 注意与失败模式**
> 要使 SBOM (或其它 attestation 元数据) 成功附着并可见对底层的存储格式有前置要求默认的 classic image store 不支持 manifest list/index 这种存放 attestation 的结构
>
> 如果只简单运行上述命令你可能会面临 **命令成功执行但本地镜像中看不到 SBOM** 的体会落差
>
> **正确的解决路径有两条**
> 1. Docker 守护进程中启用 `containerd image store` 特性现代 Docker Desktop 默认推荐
> 2. 或者使用 `docker-container` driver 的构建器并直接 **加上 `--push` 参数** 将产物推送到远端支持 OCI 的镜像仓库仓库会正确保存这些元数据
### 10.2.2 官方文档
* https://docs.docker.com/engine/reference/commandline/buildx/