本地缓存

local 缓存存储是一种简单的缓存选项,它将您的缓存存储为文件系统中的目录中的文件,使用OCI 镜像布局作为底层目录结构。如果您只是在测试,或者您希望灵活地自我管理共享存储解决方案,本地缓存是一个不错的选择。

概要

$ docker buildx build --push -t <registry>/<image> \
  --cache-to type=local,dest=path/to/local/dir[,parameters...] \
  --cache-from type=local,src=path/to/local/dir .

下表描述了可以传递给--cache-to--cache-from的可用CSV参数。

NameOptionTypeDefaultDescription
srccache-fromStringPath of the local directory where cache gets imported from.
digestcache-fromStringDigest of manifest to import, see 缓存版本控制.
destcache-toStringPath of the local directory where cache gets exported to.
modecache-tomin,maxminCache layers to export, see cache mode.
oci-mediatypescache-totrue,falsetrueUse OCI media types in exported manifests, see OCI 媒体类型.
image-manifestcache-totrue,falsefalseWhen using OCI media types, generate an image manifest instead of an image index for the cache image, see OCI 媒体类型.
compressioncache-togzip,estargz,zstdgzipCompression type, see 缓存压缩.
compression-levelcache-to0..22Compression level, see 缓存压缩.
force-compressioncache-totrue,falsefalseForcibly apply compression, see 缓存压缩.
ignore-errorcache-toBooleanfalseIgnore errors caused by failed cache exports.

如果src缓存不存在,那么缓存导入步骤将失败,但构建会继续。

缓存版本控制

本节描述了本地文件系统上缓存的版本控制是如何工作的,以及如何使用digest参数来使用旧版本的缓存。

如果您手动检查缓存目录,您可以看到生成的OCI镜像布局:

$ ls cache
blobs  index.json  ingest
$ cat cache/index.json | jq
{
  "schemaVersion": 2,
  "manifests": [
    {
      "mediaType": "application/vnd.oci.image.index.v1+json",
      "digest": "sha256:6982c70595cb91769f61cd1e064cf5f41d5357387bab6b18c0164c5f98c1f707",
      "size": 1560,
      "annotations": {
        "org.opencontainers.image.ref.name": "latest"
      }
    }
  ]
}

与其他缓存类型一样,本地缓存在导出时会被替换,通过替换index.json文件的内容。然而,之前的缓存仍然可以在blobs目录中找到。这些旧缓存可以通过摘要进行寻址,并且会无限期保留。因此,本地缓存的大小将继续增长(更多信息请参见moby/buildkit#1896)。

当使用--cache-to导入缓存时,您可以指定digest参数 以强制加载旧版本的缓存,例如:

$ docker buildx build --push -t <registry>/<image> \
  --cache-to type=local,dest=path/to/local/dir \
  --cache-from type=local,ref=path/to/local/dir,digest=sha256:6982c70595cb91769f61cd1e064cf5f41d5357387bab6b18c0164c5f98c1f707 .

进一步阅读

有关缓存的介绍,请参见 Docker 构建缓存

有关local缓存后端的更多信息,请参阅 BuildKit 自述文件