【发布时间】:2021-09-07 15:37:16
【问题描述】:
我需要在一个普通阶段的基础上构建 2 个阶段
$ ls
Dockerfile dev other prod
$ cat Dockerfile
FROM scratch as dev
COPY dev /
FROM scratch as other
COPY other /
FROM scratch as prod
COPY --from=dev /env /
COPY prod /
如您所见,prod 阶段不依赖于other 阶段,但无论如何它都会构建它
$ docker build . --target prod
Sending build context to Docker daemon 4.096kB
Step 1/7 : FROM scratch as dev
--->
Step 2/7 : COPY dev /
---> 64c24f1f1d8c
Step 3/7 : FROM scratch as other
--->
Step 4/7 : COPY other /
---> 9b0753ec4353
Step 5/7 : FROM scratch as prod
--->
Step 6/7 : COPY --from=dev /dev /
---> Using cache
---> 64c24f1f1d8c
Step 7/7 : COPY prod /
---> 9fe8cc3d3ac1
Successfully built 9fe8cc3d3ac1
为什么 Docker 需要构建 other 层?
如何在没有 other 的情况下构建 prod ?我必须使用另一个 Dockerfile 吗?
【问题讨论】:
标签: docker dockerfile docker-multi-stage-build