0

目前我有以下 Dockerfile:

FROM debian:10 as builder
RUN sleep 10
COPY input input
# worlds most trivial build pipeline
RUN cat input > artifact

FROM debian:10
COPY --from=builder artifact artifact
RUN cat artifact
COPY input2 input2

我有 docker-compose 文件,如下所示:

%YAML 1.1
---
version: '3.7'
services:
  sdn-controller:
    build:
      context: .
      cache_from:
      - hansbogert/test1:latest
      args:
      - BUILDKIT_INLINE_CACHE=1
    image: hansbogert/test1:latest

还有两个空文件:

$ touch input input2

如果我使用 buildkit 进行初始构建,并将它们推送到注册表:

export DOCKER_BUILDKIT=1 
docker-compose build 
docker-compose push

在撰写文件中牢记缓存来源:

案例1)我希望当我没有本地缓存​​时,即

docker rmi hansbogert/test1 ; docker image prune -f; docker builder prune -af

一个新的构建将被完全缓存,(它是):

docker-compose build 

[trunc'd, but all cached]

案例2)我希望如果我编辑文件,那么只有docker文件中的最新阶段需要重做,input2COPY-ingof开始,它会:input2

$ echo 1 > input2
$ docker rmi hansbogert/test1 ; docker image prune -f; docker builder prune -af
$ docker-compose build 
[trunc'd]
=> CACHED [stage-1 3/4] RUN cat artifact                                                                                            0.7s
 => => pulling sha256:f33c84f9d3c6505acdda2a6d1c7238c853e07f3723e4a5d4c9eb65a163710ffd                                               0.3s
 => => pulling sha256:0b00a0a96175fa32a06c3741ac1fb655aafc2ed1584eebfd2e213561998f7bea                                               0.4s
 => [stage-1 4/4] COPY input2 input2                                                                                                 0.0s
 => exporting to image                                                                                                           
...

案例3)当我编辑input文件时,我希望第一阶段被缓存到该COPY input input行,唉,这不会发生,由 uncacheRUN sleep 10行发出信号:

$ echo 1 > input
$ docker rmi hansbogert/test1 ; docker image prune -f; docker builder prune -af
$ docker-compose build 

=> [internal] load build definition from Dockerfile                                                                                 0.0s
 => => transferring dockerfile: 254B                                                                                                 0.0s
 => [internal] load .dockerignore                                                                                                    0.1s
 => => transferring context: 2B                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/debian:10                                                                         0.0s
 => importing cache manifest from hansbogert/test1:latest                                                                            1.3s
 => [builder 1/4] FROM docker.io/library/debian:10                                                                                   0.1s
 => [internal] load build context                                                                                                    0.0s
 => => transferring context: 71B                                                                                                     0.0s
 => [builder 2/4] RUN sleep 10                                                                                                      10.5s
 => [builder 3/4] COPY input input                                                                                                   0.1s
 => [builder 4/4] RUN echo input > artifact                                                                                          0.6s
 => CACHED [stage-1 2/4] COPY --from=builder artifact artifact                                                                       0.0s
[trunc'd, but note  that this stage is cached as much as possible! ]

主要问题:有没有办法为最终图像中的阶段以外的阶段进行缓存?

github上好像有相关问题

然而,它的结论似乎并没有解决我的上述问题。

4

1 回答 1

0

目前,如果不使用 buidlx 或“纯”构建套件,这是不可能的。内联缓存,这是您在 buildkit 术语中使用的内容,无法为所有阶段提供缓存,如buildctl 的 README中的以下段落所示:

在大多数情况下,您希望使用内联缓存导出器。但是请注意,内联缓存导出器仅支持最小缓存模式。要启用最大缓存模式,请使用注册表缓存导出器分别推送图像和缓存。

因此,您需要 buildkit 的cache-to="type=registry,mode=max".

于 2021-11-06T19:13:15.313 回答