注册表缓存

registry 缓存存储可以被视为 inline 缓存的扩展。与 inline 缓存不同,registry 缓存完全独立于镜像,这使得其使用更加灵活 - registry 支持的缓存可以完成 inline 缓存所能做的一切,甚至更多:

  • 允许分离缓存和生成的图像工件,以便您可以在不包含缓存的情况下分发最终图像。
  • 它可以在max模式下高效地缓存多阶段构建,而不仅仅是最终阶段。
  • 它与其他导出器一起工作以提供更大的灵活性,而不仅仅是image导出器。

默认的docker驱动程序不支持此缓存存储后端。 要使用此功能,请使用不同的驱动程序创建一个新的构建器。有关更多信息,请参阅 构建驱动程序

概要

与更简单的inline缓存不同,registry缓存支持多个配置参数:

$ docker buildx build --push -t <registry>/<image> \
  --cache-to type=registry,ref=<registry>/<cache-image>[,parameters...] \
  --cache-from type=registry,ref=<registry>/<cache-image> .

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

NameOptionTypeDefaultDescription
refcache-to,cache-fromStringFull name of the cache image to import.
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 cache compression.
force-compressioncache-totrue,falsefalseForcibly apply compression, see 缓存压缩.
ignore-errorcache-toBooleanfalseIgnore errors caused by failed cache exports.

你可以为ref选择任何有效的值,只要它与你要推送镜像的目标位置不同。你可以选择不同的标签(例如foo/bar:latestfoo/bar:build-cache),不同的镜像名称(例如foo/barfoo/bar-cache),甚至不同的仓库(例如docker.io/foo/barghcr.io/foo/bar)。由你决定使用哪种策略来将你的镜像与缓存镜像分开。

如果--cache-from目标不存在,那么缓存导入步骤将会失败,但构建会继续。

进一步阅读

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

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