Assume export DOCKER_BUILDKIT=1.
Take main.py:
i = 0
while True:
i += 1
Take this Dockerfile:
FROM python:3.9-slim as base
COPY main.py .
FROM base as part_1
RUN echo "A" && python -m main
FROM base as part_2
RUN echo "B" && python -m main
FROM base as combined
COPY --from=part_1 . .
COPY --from=part_2 . .
Running docker build --no-cache . followed by top shows that the build is being parallelized to take 2 cores, expected from BuildKit:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
22569 root 20 0 14032 11620 4948 R 100.0 0.0 0:10.43 python
22571 root 20 0 14032 11620 4948 R 100.0 0.0 0:10.34 python
But removing the echos from the Dockerfile:
FROM python:3.9-slim as base
COPY main.py .
FROM base as part_1
RUN python -m main
FROM base as part_2
RUN python -m main
FROM base as combined
COPY --from=part_1 . .
COPY --from=part_2 . .
and rerunning docker build --no-cache . followed by top shows that the build is only taking one core (with the second process being an irrelevant one), unexpected from BuildKit:
PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND
24674 root 20 0 14032 11624 4952 R 100.0 0.0 1:00.40 python
2485 mishac 20 0 5824548 515428 126120 S 12.3 1.6 2:52.74 gnome-s+
Why is the version without the echos disabling the parallelization? It seems like an odd thing to be affecting it. Is it possible to keep the parallelization without the echos?
Version:
$ docker --version
Docker version 20.10.16, build aa7e414

