这是我的Dockerfile
:
FROM alpine:latest as builder
RUN touch /builder
FROM alpine:latest as app
RUN touch /app
CMD ["bash"]
第一次构建会拉起alpine
并建立builder
:
$ docker build -t builder --cache-from builder --target builder .
Sending build context to Docker daemon 14.85kB
Step 1/2 : FROM alpine:latest as builder
latest: Pulling from library/alpine
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
---> 961769676411
Step 2/2 : RUN touch /builder
---> Running in d142125feb20
Removing intermediate container d142125feb20
---> 1adad9638a6c
Successfully built 1adad9638a6c
Successfully tagged builder:latest
拉动alpine
,构建builder
和构建app
的第二次构建使用:
$ docker build -t app --cache-from builder --cache-from app --target app .
Sending build context to Docker daemon 14.85kB
Step 1/5 : FROM alpine:latest as builder
---> 961769676411
Step 2/5 : RUN touch /builder
---> Using cache
---> 1adad9638a6c
Step 3/5 : FROM alpine:latest as app
---> 961769676411
Step 4/5 : RUN touch /app
---> Running in 702ee875f781
Removing intermediate container 702ee875f781
---> d2b010f12ee8
Step 5/5 : CMD ["bash"]
---> Running in 6c7a6354b8bf
Removing intermediate container 6c7a6354b8bf
---> e375b0b7905e
Successfully built e375b0b7905e
Successfully tagged app:latest
现在,我在本地删除了alpine
:
$ docker image rm alpine
Untagged: alpine:latest
Untagged: alpine@sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
我的期望是:使用--cache-from
进行构建就足够了。
但是,alpine
再次被拉了。如果更改了它,则会使我的缓存失效。
$ docker build -t app --cache-from builder --cache-from app --target app .
Sending build context to Docker daemon 14.85kB
Step 1/5 : FROM alpine:latest as builder
latest: Pulling from library/alpine
Digest: sha256:72c42ed48c3a2db31b7dafe17d275b634664a708d901ec9fd57b1529280f01fb
Status: Downloaded newer image for alpine:latest
---> 961769676411
Step 2/5 : RUN touch /builder
---> Using cache
---> 1adad9638a6c
Step 3/5 : FROM alpine:latest as app
---> 961769676411
Step 4/5 : RUN touch /app
---> Using cache
---> d2b010f12ee8
Step 5/5 : CMD ["bash"]
---> Using cache
---> e375b0b7905e
Successfully built e375b0b7905e
Successfully tagged app:latest
问题:
builder
作为 的基础映像,因此在构建带有两个app
的{{1}}时(一个用于--cache-from
,另一个用于{{1 }})就足够了,正如我的期望吗?因此,如果不存在,builder
就不需要app
吗?pull
),以下内容会更好吗?代替使用alpine
,使用1.
吗?还是FROM alpine:latest
?我要确保从其他计算机进行构建时的缓存。我不知道哪个标签是“可靠的”,因此它不会更改:即,他们在推入FROM alpine:3.10
时更新3.10.2
,因为3
是3.10
的一部分。意思是3.10
不会改变?除非会有3
:/ 3.10.2
作为基本图片,目前找不到3.10.2.2
之类的东西。因此,没有其他要中继的特定图像。为什么困扰我:因为我将这些图像存储在我的docker存储库中。因此,我还需要存储一份alpine副本-我曾经构建的副本。假设我称它为ubuntu:18.04
。那么我的构建应该是:
ubuntu:18.04.3
谢谢