跳到主要内容

buildkit

提示
apk add docker-cli-buildx # AlpineLinux

docker buildx version
docker buildx use default
docker buildx install # buildx -> build

# 额外的 Builder
docker buildx create --use
docker build -t demo -o type=image .

# Test
mkdir -p /tmp/build
cd /tmp/build
cat << EOF > Dockerfile
FROM wener/base
EOF
docker buildx build -t test .
docker buildx build --platform linux/amd64,linux/arm64 --pull --push -t test .
  • driver
    • docker
    • docker-container
    • kubernetes
    • remote

platform

  • amd64, arm32v5, arm32v6, arm32v7, arm64v8, i386, ppc64le, s390x
docker run --privileged --rm tonistiigi/binfmt --install all

output

  • type=image,name=REGISTRY/IMAGE,push=true,compression=zstd
  • compression-level
    • 0-9 - gzip,estargz
    • 0-22 - zstd
  • force-compression=true
  • push=true
  • oci-mediatypes=true
  • buildinfo=true
    • 附加构建信息 {"moby.buildkit.buildinfo.v0": "<base64>"}

cache

  • type
    • inline
    • registry
      • type=registry,ref=<registry>/<cache-image>[,parameters...]
    • local
    • gha
    • s3
    • azblob
  • --cache-to, --cache-from
    • 可以指定多个
    • mode=min - 默认
      • 缓存 export 最终 layer
    • mode=max
      • 缓存所有 layer
    • compression=zstd
    • oci-mediatypes=true
      • 只针对 cache-to
  • /var/lib/buildkit/runc-overlayfs/snapshots/snapshots/1639/fs/
    • cache source

RUN mount

  • cache - 创建一个目录用于缓存
    • /var/lib/buildkit/ - 在 buildkitd 容器里
  • bind - bind host 目录
    • source 为 host path - 默认 =target
    • 不同环境 source 可能不同,使用上不太方便
  • secret
  • ssh
FROM wener/node

RUN --mount=type=cache,target=/root/.cache/go-build \
go build
RUN --mount=type=cache,target=/root/.m2 \
mvn -o install
RUN --mount=type=cache,target=/root/.npm/_cacache/ \
npm install

# npm & pnpm
RUN --mount=type=cache,target=/root/.npm/_cacache/ \
--mount=type=cache,target=/root/.local/share/pnpm/store npm install -g pnpm \
&& pnpm install
type=cacheval
id=target
targetmount point
ro,readonly
sharingshared - 可选 shared,private,locked
frombuild stage, 默认为空目录
sourcefrom 的子目录
mode0755
uid0
gid0

FAQ

docker build unknown flag: --push

需要启用 buildx

cache export feature is currently not supported for docker driver

multiple platforms feature is currently not supported for docker driver. Please switch to a different driver (eg. "docker buildx create --use")
docker buildx create --use

push and load may not be set together at the moment

error mounting cache no such file or directory