19

I have a rather large Dockerfile that has multiple layers. Some of these layers need quite some time. I noticed that many things do not depend on each other.

So hence the obvious question: Can I docker build in parallel?

The docker build only seems to have options that limit the build speed, instead of speeding it up (e.g. --memory).

Example: Imagine you have a Dockerfile that looks like this. I now want to call a docker build --some-flag that builds all stages in parallel unless they have to work for each other.

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

Do you know if --some-flag exists? Do you know a different way how do achieve the goal?

EDIT:

The only thing I can think about is splitting the Dockerfile up in many more stages and thus making modifications less painful, but this is not really an ideal solution

2
  • 8
    "The latest Docker versions come with new opt-in builder backend BuildKit. While all the patterns here work with the older builder as well, many of them run much more efficiently when BuildKit backend is enabled. For example, BuildKit efficiently skips unused stages and builds stages concurrently when possible" medium.com/@tonistiigi/…
    – Tarum
    Commented May 21, 2020 at 13:48
  • Very cool! I am suprised that this was posted in 2018. Have never heard about it. Commented May 21, 2020 at 14:05

2 Answers 2

22

Thanks at m303945 for this answer!

This answer gives you an example how to build in parallel:

Dockerfile:

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"

Sequential: docker build . will take around 30 seconds.

Parallel: DOCKER_BUILDKIT=1 docker build . will take around 10 seconds.

Sources:

docker documentation

amazing blog post

1
  • 4
    The Dockerfile builds in 10 sec because docker build . only builds the last stage, i.e. s3. But, if s3 also did COPY --from=s1 / /s1 and COPY --from=s2 / /s2 then s1 and s2 would run in parallell. I'll post an answer with more details.
    – gustafc
    Commented Jan 25, 2023 at 18:46
10

If BuildKit sees that a stage depends on other stages which do not depend on each other, then it will run those stages in parallel (assuming there are enough threads/CPUs available, anyway). Here's an example:

# 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

If you build this with docker build . (which implicitly builds the last stage in the Dockerfile), stages s1, s2 and s3 will automatically run in parallel (again, assuming you have the system resources, but it's what happens on my pretty old computer). The entire build finishes in little over 10 seconds...

$ 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

... and if we run the image, we can see the stages did indeed run at the same time:

$ 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

So to answer your question, you could split stage3 into two stages, where one does the actual work, and the other just copies stuff from other stages:

FROM yetAnotherImg As stage3-intermediate
# more work

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.