目前我有以下 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文件中的最新阶段需要重做,input2
从COPY-ing
of开始,它会: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上好像有相关问题
然而,它的结论似乎并没有解决我的上述问题。