I used to build docker image step by step, that’s before I found out about docker’s BuildKit. With the
BuildKit, the building time was reduced by 60% (in my case), I mean that’s crazy.
Just split your Dockerfile into stages, the docker BuildKit will analyse all steps’ dependency of each other, and run multiple stages parallel. If a step does not depend on any other step, it will go straight forward. If a step depends on any other step, it will wait for its prerequisite to finish.
Here’s a piece of Dockerfile from my project:
All stages started at the same time, while step 3 of stage-2 waited for stage-0 and stage-1 to finish, because the
--from=0 flag of
COPY command tells the step to wait for the stage-0 and stage-1 to finish. Thus, I saved a lot of time.
There are a few ways to use BuildKit:
# Enable BuildKit for docker and docker-compose
Please see official documentation for more details.