docker可以并行构建多阶段映像吗?

xxls0lw8  于 2023-01-29  发布在  Docker
关注(0)|答案(2)|浏览(153)

我有一个相当大的Dockerfile,它有多个层。其中一些层需要相当长的时间。我注意到很多事情并不相互依赖。
因此,**显而易见的问题是:**我可以并行docker build吗?
docker build似乎只有限制构建速度的选项,而不是加速构建速度(eidogg. --memory)。
例如:假设你有一个Dockerfile,看起来像这样,我现在想调用一个docker build --some-flag,它并行地构建所有的阶段,除非它们必须互相工作。

FROM someImage AS stage1
# do some task

FROM otherImage AS stage2
# do another taks

FROM yetAnotherImg As stage3
# more work
COPY --from=stage2 ... # stage3 has to wait for stage2 to finish

你知道--some-flag是否存在吗?你知道如何实现这个目标的不同方法吗?

    • 编辑:**

我唯一能想到的是将Dockerfile分割成更多的阶段,从而减少修改的痛苦,但这并不是一个理想的解决方案

hyrbngr7

hyrbngr71#

感谢m303945的回答!
这个答案为您提供了一个如何并行构建的示例:

    • 停靠文件:**
FROM alpine as s1
RUN sleep 10 && echo "s1 done"

FROM alpine as s2
RUN sleep 10 && echo "s2 done"

FROM alpine as s3
RUN sleep 10 && echo "s3 done"

顺序:docker build .大约需要30秒。
并行:DOCKER_BUILDKIT=1 docker build .大约需要10秒。

    • 资料来源:**

码头工文件
amazing blog post

eaf3rand

eaf3rand2#

如果BuildKit发现一个阶段依赖于其他不相互依赖的阶段,那么它将并行运行这些阶段(无论如何,假设有足够的线程/CPU可用)。

# syntax=docker/dockerfile:1.4

FROM alpine as s1
RUN echo Stage s1 building, time is `date` >/s1.txt && sleep 10

FROM alpine as s2
RUN echo Stage s2 building, time is `date` >/s2.txt && sleep 10

FROM alpine as s3
RUN echo Stage s3 building, time is `date` >/s3.txt && sleep 10

FROM s3 AS final-stage
COPY --from=s1 /s1.txt /
COPY --from=s2 /s2.txt /
ENTRYPOINT cat /s*.txt

如果您使用docker build .构建这个代码(它隐式地构建Dockerfile中的最后一个阶段),则s1s2s3阶段将自动并行运行(同样,假设您有系统资源,但这是在我的相当旧的计算机上发生的)。

$ docker build .
[+] Building 12.6s (14/14) FINISHED                                                                                                                                                                         
 => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
 => => transferring dockerfile: 419B                                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => => transferring context: 2B                                                                                                                                                                        0.0s
 => resolve image config for docker.io/docker/dockerfile:1.4                                                                                                                                           1.6s
 => CACHED docker-image://docker.io/docker/dockerfile:1.4@sha256:9ba7531bd80fb0a858632727cf7a112fbfd19b17e94c4e84ced81e24ef1a0dbc                                                                      0.0s
 => [internal] load build definition from Dockerfile                                                                                                                                                   0.0s
 => [internal] load .dockerignore                                                                                                                                                                      0.0s
 => [internal] load metadata for docker.io/library/alpine:latest                                                                                                                                       0.0s
 => CACHED [s2 1/2] FROM docker.io/library/alpine                                                                                                                                                      0.0s
 => [s1 2/2] RUN echo Stage s1 building, time is `date` >/s1.txt && sleep 10                                                                                                                          10.4s
 => [s3 2/2] RUN echo Stage s3 building, time is `date` >/s3.txt && sleep 10                                                                                                                          10.5s
 => [s2 2/2] RUN echo Stage s2 building, time is `date` >/s2.txt && sleep 10                                                                                                                          10.4s
 => [final-stage 1/2] COPY --from=s1 /s1.txt /                                                                                                                                                         0.0s
 => [final-stage 2/2] COPY --from=s2 /s2.txt /                                                                                                                                                         0.0s
 => exporting to image                                                                                                                                                                                 0.0s
 => => exporting layers                                                                                                                                                                                0.0s
 => => writing image sha256:3f76475f4bab8aa773a7b4daf1403cbd269fa964b7133f81800e1359eae08d60                                                                                                           0.0s

Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them

......如果我们运行图像,我们可以看到各个阶段确实是同时运行的:

$ docker run --rm sha256:3f76475f4bab8aa773a7b4daf1403cbd269fa964b7133f81800e1359eae08d60
Stage s1 building, time is Wed Jan 25 18:52:50 UTC 2023
Stage s2 building, time is Wed Jan 25 18:52:50 UTC 2023
Stage s3 building, time is Wed Jan 25 18:52:50 UTC 2023

为了回答您的问题,您可以将stage3分为两个阶段,其中一个阶段执行实际工作,另一个阶段只是从其他阶段复制内容:

FROM yetAnotherImg As stage3-intermediate
# more work

FROM stage3-intermediate AS stage3
COPY --from=stage2 ... # stage2 and stage3-intermediate will run in parallel

相关问题