docker buildx 设置构建缓存策略(设置缓存保存时间及大小上限)

关键词:docker buildx cache gc prune 构建 缓存 策略 缓存时间 缓存大小

本文同步发表在我的个人博客https://evine.win。

新建buildkit配置文件$HOME/.docker/buildx/buildkitd.default.toml如下:

[worker.oci]
  enabled = true
  gc = true
  gckeepstorage = 20480     # 单位:MiB
  [[worker.oci.gcpolicy]]
    keepBytes = 21474836480 # 单位:B,下同
    keepDuration = 7200     # 单位:s,下同
    filters = [ "type==source.local", "type==exec.cachemount", "type==source.git.checkout"]
  [[worker.oci.gcpolicy]]
    keepBytes = 32212254720
    keepDuration = 86400
    filters = [ "type==source.local", "type==exec.cachemount", "type==source.git.checkout"]
  [[worker.oci.gcpolicy]]
    all = true
    keepBytes = 42949672960

上述配置可以按时间、按上限设置多个层级的worker.oci.gcpolicy,按照不同时间、不同上限来删除buildx的构建缓存。如果是本地构建,主要的缓存类型是source.localexec.cachemount。详细配置请见:buildkit。

如果之前创建过名为builder的实例,先删除:

docker buildx rm builder

然后重新创建一个名为builder的实例:

docker run --privileged --rm tonistiigi/binfmt --install all
docker buildx create --name builder --driver=docker-container --use --bootstrap
docker buildx inspect

上面最后一个命令docker buildx inspect输出如下:

Name:          builder
Driver:        docker-container
Last Activity: 2023-09-06 02:55:47 +0000 UTC

Nodes:
Name:      builder0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Buildkit:  v0.12.1
Platforms: linux/amd64, linux/amd64/v2, linux/amd64/v3, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6
Labels:
 org.mobyproject.buildkit.worker.executor:         oci
 org.mobyproject.buildkit.worker.hostname:         bcff51b12dca
 org.mobyproject.buildkit.worker.network:          host
 org.mobyproject.buildkit.worker.oci.process-mode: sandbox
 org.mobyproject.buildkit.worker.selinux.enabled:  false
 org.mobyproject.buildkit.worker.snapshotter:      overlayfs
GC Policy rule#0:
 All:           false
 Filters:       type==source.local type==exec.cachemount type==source.git.checkout
 Keep Duration: 2h0m0s
 Keep Bytes:    20GiB
GC Policy rule#1:
 All:           false
 Filters:       type==source.local type==exec.cachemount type==source.git.checkout
 Keep Duration: 24h0m0s
 Keep Bytes:    30GiB
GC Policy rule#2:
 All:        true
 Keep Bytes: 40GiB

可以看出缓存策略已经设置为自己的了。

你可能感兴趣的:(docker buildx 设置构建缓存策略(设置缓存保存时间及大小上限))