–cache-to/from和–mount type=cache在docker buildx build中的区别是什么?

huangapple go评论81阅读模式
英文:

Difference between --cache-to/from and --mount type=cache in docker buildx build

问题

根据官方文档,为了在docker buildx build中利用缓存后端,您需要使用--cache-from/to标志。

这有道理,因为直观地表示构建结果将被缓存到的地方(--cache-to),以及用于加速构建过程的缓存(--cache-from)。

然而,还有另一种使用RUN 指令 中的mount选项来使用缓存的替代方式(官方示例如下):

RUN \
    --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -y git

在最后一个示例中,这些(在我们的情况下是apt包)将从哪里获取?

缓存后端(例如s3gha等)是否适用于这种情况?

这两种情况是互补的还是正交的?

英文:

According to the official documentation, in order to leverage a cache backend in docker buildx build, you need to use the --cache-from/to flags.

This makes sense as intuitively it signifies the place where the build result will be cached to (--cache-to) and what cache it will use to speed up the build process (--cache-from).

However, there is another alternative (?) of using cache with the mount option within the RUN directive, as in: (official example)

RUN \
    --mount=type=cache,target=/var/cache/apt \
    apt-get update && apt-get install -y git

In the last example where these (apt packages in our case) will be retrieved from?

Is the cache backend (s3, gha etc) applicable in this case?

Are these two cases complementary or orthogonal?

答案1

得分: 2

--cache-to/from 用于存储构建步骤的结果,并在将来的构建中重用它,避免再次运行命令。这些结果存储在构建器之外的持久位置,比如注册表,这样其他构建器即使在本地系统上没有构建镜像也可以跳过已经完成的构建步骤。

--mount type=cache 在临时容器内创建一个挂载点,该挂载点在运行步骤中执行。当步骤本身未缓存时,此挂载点在后续构建执行中被重用。当一个步骤下载了许多不需要包含在镜像中的外部依赖项,并且可以在构建之间安全地重用时,这将非常有用。挂载点缓存的存储是在构建器本地的,并在首次使用时是一个空目录。

英文:

They are solving two different problems.

--cache-to/from is used to store the result of a build step and reuse it in future builds, avoiding the need to run the command again. This is stored in a persistent location outside of the builder, like on a registry, so that other builders can skip already completed steps of a build even if the image wasn't built on the local system.

--mount type=cache creates a mount inside the temporary container that's executed in a RUN step. This mount is reused in later executions of the build when the step itself is not cached. This is useful when a step pulls down a lot of external dependencies that do not need to be in the image and can safely be reused between builds. The storage of the mount cache is local to the builder and is an empty directory on first use.

huangapple
  • 本文由 发表于 2023年5月28日 20:02:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76351391.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定