如何使用多级Dockerfile获得大量缓存



目前我有以下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)我希望,如果我编辑input2文件,只有docker文件中的最新阶段,从input2COPY-ing开始需要重做,它做的:

$ 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上似乎有一个相关的问题

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

目前不使用buildlx或'pure' buildkit是不可能的。内联缓存,这是你在buildkit术语中使用的,不能为所有阶段提供缓存,如下段所示,来自buildctl的README:

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

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

相关内容

  • 没有找到相关文章

最新更新