1、docker buildx build 简介
docker buildx build命令使用BuildKit启动构建。这个命令类似于docker build命令的UI,使用相同的标志和参数。
参考文档:https://docs.docker.com/engine/reference/commandline/buildx_build/
2、docker buildx build 语法
docker buildx build [OPTIONS] PATH | URL | -
3、docker buildx build 命令
1)允许额外的特权(--allow)
network.host:允许执行主机网络。
security.insecure:允许执行没有sandbox的命令。
docker buildx create --use --name insecure-builder --buildkitd-flags '--allow-insecure-entitlement security.insecure'
docker buildx build --allow security.insecure .2)设置构建时变量(--build-arg)
BUILDKIT_CONTEXT_KEEP_GIT_DIR=: 触发git上下文,保留.git目录。
BUILDKIT_INLINE_BUILDINFO_ATTRS=:是否在镜像配置中内联构建信息属性
BUILDKIT_INLINE_CACHE= inline cache: 是否配置元数据
BUILDKIT_MULTI_PLATFORM=: 选择确定性输出,而不管是否支持多平台输出。
docker buildx build --build-arg BUILDKIT_MULTI_PLATFORM=1 .
3)额外的构建上下文 (--build-context)
docker buildx build --build-context alpine=docker-image://alpine@sha256:0123456789 .
docker buildx build --build-context project=path/to/project/source .docker buildx build --build-context foo=oci-layout:///path/to/local/layout@sha256:abcd12345 .4)使用外部缓存源进行构建(--cache-from)
registry :可以从注册表上的缓存清单或(特殊)镜像配置导入缓存。
local :可以从之前使用--cache-to导出的本地文件导入缓存。
gha:可以在GitHub仓库中使用--cache-to从之前导出的缓存中导入缓存。
docker buildx build --cache-from=user/app:cache .
docker buildx build --cache-from=user/app .
docker buildx build --cache-from=type=registry,ref=user/app .
docker buildx build --cache-from=type=local,src=path/to/cache .
docker buildx build --cache-from=type=gha .5)将构建缓存导出到外部缓存目标(--cache-to)
registry: 将构建缓存导出到registry中的缓存清单。
local:Type在客户端将缓存导出到本地目录。
inline: Type将缓存元数据写入映像配置。
gha: Type通过Github Actions cache service API导出缓存。
 docker buildx build --cache-to=user/app:cache .
 docker buildx build --cache-to=type=inline .
 docker buildx build --cache-to=type=registry,ref=user/app .
 docker buildx build --cache-to=type=local,dest=path/to/cache .
 docker buildx build --cache-to=type=gha .6)将构建结果元数据写入文件 (--metadata-file)
docker buildx build --load --metadata-file metadata.json . cat metadata.json
{
  "containerimage.buildinfo": {
    "frontend": "dockerfile.v0",
    "attrs": {
      "context": "https://github.com/crazy-max/buildkit-buildsources-test.git#master",
      "filename": "Dockerfile",
      "source": "docker/dockerfile:master"
    },
    "sources": [
      {
        "type": "docker-image",
        "ref": "docker.io/docker/buildx-bin:0.6.1@sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0",
        "pin": "sha256:a652ced4a4141977c7daaed0a074dcd9844a78d7d2615465b12f433ae6dd29f0"
      },
      {
        "type": "docker-image",
        "ref": "docker.io/library/alpine:3.13",
        "pin": "sha256:026f721af4cf2843e07bba648e158fb35ecc876d822130633cc49f707f0fc88c"
      }
    ]
  },
  "containerimage.config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
  "containerimage.descriptor": {
    "annotations": {
      "config.digest": "sha256:2937f66a9722f7f4a2df583de2f8cb97fc9196059a410e7f00072fc918930e66",
      "org.opencontainers.image.created": "2022-02-08T21:28:03Z"
    },
    "digest": "sha256:19ffeab6f8bc9293ac2c3fdf94ebe28396254c993aea0b5a542cfb02e0883fa3",
    "mediaType": "application/vnd.oci.image.manifest.v1+json",
    "size": 506
  },
  "containerimage.digest": "sha256:19ffeab6f8bc9293ac2c3fdf94ebe28396254c993aea0b5a542cfb02e0883fa3"
}7)为构建结果设置导出操作(-o, --output)
docker buildx build -o . .
docker buildx build -o outdir .
docker buildx build -o - - > out.tar
docker buildx build -o type=docker .
docker buildx build -o type=docker,dest=- . > myimage.tar
docker buildx build -t tonistiigi/foo -o type=registry8)为构建设定目标平台(--platform)
docker buildx build --platform=linux/arm64 . docker buildx build --platform=linux/amd64,linux/arm64,linux/arm/v7 . docker buildx build --platform=darwin .
9)设置进度输出类型(--progress)
设置进度输出类型(auto、plain、tty)。使用plain来显示容器输出(默认为“auto”)。
docker buildx build --load --progress=plain .
#1 [internal] load build definition from Dockerfile
#1 transferring dockerfile: 227B 0.0s done
#1 DONE 0.1s
#2 [internal] load .dockerignore
#2 transferring context: 129B 0.0s done
#2 DONE 0.0s
...10)要暴露给构建的Secret(--secret)
文件:
# syntax=docker/dockerfile:1.4
FROM python:3
RUN pip install awscli
RUN --mount=type=secret,id=aws,target=/root/.aws/credentials \
  aws s3 cp s3://... ...docker buildx build --secret id=aws,src=$HOME/.aws/credentials .环境变量:
# syntax=docker/dockerfile:1.4
FROM node:alpine
RUN --mount=type=bind,target=. \
  --mount=type=secret,id=SECRET_TOKEN \
  SECRET_TOKEN=$(cat /run/secrets/SECRET_TOKEN) yarn run testSECRET_TOKEN=token docker buildx build --secret id=SECRET_TOKEN .
11)SSH代理套接字或密钥暴露给构建(--ssh)
# syntax=docker/dockerfile:1.4
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 -q -T git@gitlab.com 2>&1 | tee /hello
# "Welcome to GitLab, @GITLAB_USERNAME_ASSOCIATED_WITH_SSHKEY" should be printed here
# with the type of build progress is defined as `plain`.eval $(ssh-agent)
ssh-add ~/.ssh/id_rsa
(Input your passphrase here)
docker buildx build --ssh default=$SSH_AUTH_SOCK .12)ulimit设置
格式为:=[:]
docker buildx build --ulimit nofile=1024:1024 .4、选项说明
| 名称, 简称 | 默认 | 描述 | 
| --add-host | 添加自定义主机到ip映射 (格式: host:ip) | |
| --allow | 允许额外的特权 (如,network.host, security.insecure) | |
| --build-arg | 设置构建时变量 | |
| --build-context | 额外的构建上下文 (如,name=path) | |
| --cache-from | 外部缓存源 (如, user/app:cache, type=local,src=path/to/dir) | |
| --cache-to | 缓存导出目的地 (如,user/app:cache, type=local,dest=path/to/dir) | |
| --cgroup-parent | 容器的可选父cgroup | |
| --compress | 使用gzip压缩构建上下文 | |
| --cpu-period | 限制CPU CFS(完全公平调度器)周期 | |
| --cpu-quota | 限制CPU CFS(完全公平调度器)配额 | |
| --cpu-shares , -c | CPU份额(相对权重) | |
| --cpuset-cpus | 允许执行的cpu (0-3, 0,1) | |
| --cpuset-mems | 允许执行的MEMs (0-3, 0,1) | |
| --file , -f | Dockerfile的名称 (默认: PATH/Dockerfile) | |
| --force-rm | 始终删除中间容器 | |
| --iidfile | 将镜像ID写入文件 | |
| --invoke | 在构建后调用命令 [experimental] | |
| --isolation | 容器隔离技术 | |
| --label | 设置镜像的元数据 | |
| --load | --output=type=docker的简写 | |
| --memory , -m | 限制内存 | |
| --memory-swap | Swap limit等于memory加上Swap: -1,表示无限制交换 | |
| --metadata-file | 将构建结果元数据写入文件 | |
| --network | 在构建期间为RUN指令设置网络模式 | |
| --no-cache | 在构建镜像时不使用缓存 | |
| --no-cache-filter | 不缓存指定的阶段 | |
| --output , -o | 输出目的地 (格式: type=local,dest=path ) | |
| --platform | 设定目标平台进行构建 | |
| 打印信息请求结果 (如, outline, targets) [experimental] | ||
| --progress | auto | 设置进度输出类型(auto、plain、tty)。 使用plain来显示容器输出 | 
| --pull | 始终尝试提取所有引用的镜像 | |
| --push | --output=type=registry的简写 | |
| --quiet , -q | 关闭构建输出并在成功时打印镜像ID | |
| --rm | true | 在成功构建后移除中间容器 | 
| --secret | 要暴露给构建的secret ((格式: id=mysecret[,src=/local/secret])) | |
| --security-opt | 安全选项 | |
| --shm-size | /dev/shm的大小 | |
| --squash | 将新建的层压缩为一个新层 | |
| --ssh | SSH代理套接字或密钥暴露给构建 (格式:ssh=default|[=|[,]]) | |
| --tag , -t | 名称和可选的标签 (格式: name:tag) | |
| --target | 设定要构建的目标构建阶段 | |
| --ulimit | Ulimit选项 | |
| --builder | 覆盖已配置的builder实例 | 
5、相关命令
| 命令 | 描述 | 
| docker buildx bake | 从文件构建 | 
| docker buildx build | 开始构建 | 
| docker buildx create | 创建一个新的builder实例 | 
| docker buildx du | 磁盘使用情况 | 
| docker buildx imagetools | 在registry中处理镜像的命令 | 
| docker buildx inspect | 查当前builder实例 | 
| docker buildx ls | 列出 builder 实现 | 
| docker buildx prune | 移除构建缓存 | 
| docker buildx rm | 删除 builder 实现 | 
| docker buildx stop | 停止 builder 实现 | 
| docker buildx use | 设置当前 builder 实例 | 
| docker buildx version | 显示buildx版本信息 | 
6、父命令
| 命令 | 描述 | 
| Docker Buildx |