Speed Up Docker Builds with BuildKit: A Beginner’s Guide

NHAILA Achraf
4 min readMay 28, 2024

Introduction

with Docker’s BuildKit and Buildx, you can significantly reduce build times and improve efficiency. This beginner-friendly guide will show you the differences between traditional Docker builds and builds using BuildKit, with an examples to demonstrate the time savings.

What is BuildKit?

BuildKit is an advanced build engine for Docker images, offering features such as:

  • Parallel Builds: Execute multiple stages of a Dockerfile simultaneously.
  • Improved Caching: Efficiently use and manage build caches.
  • Remote Caching: Use cache from remote sources.
  • Flexible Frontends: Support for multiple build frontends.

What is Buildx?

Buildx is a Docker CLI plugin that enhances Docker’s build capabilities with BuildKit. It offers:

  • Multi-Platform Builds: Build images for multiple platforms from a single Dockerfile.
  • Advanced Caching: Export and import build caches.
  • Custom Builders: Create and manage multiple builder instances.
  • Seamless Integration: Works seamlessly with Docker Hub and other registries.

Benefits of Using BuildKit and Buildx

  • Faster Builds: Reduced build times due to parallel execution and better caching.
  • Flexibility: Support for multi-platform builds and advanced caching mechanisms.
  • Efficiency: Optimized resource usage through improved cache management.
  • Scalability: Easily handle complex build processes.

Example Application: Traditional vs. BuildKit Builds

Let’s compare building a Docker image using the traditional docker build command and the BuildKit-enhanced docker buildx build command. We'll use a simple Python application as an example.

Step 1: Python application

just pull the project from github

git clone https://github.com/achrafnh/demo-buildkit.git
cd demo-buildkit

Step 2: Build the Image Without BuildKit

First, let’s measure the time it takes to build the Docker image using the traditional method:

time docker build -t myapp:latest .

Step 3: Enable BuildKit

Enable BuildKit by setting the environment variable:

export DOCKER_BUILDKIT=1

Step 4: Create and Use a Buildx Builder Instance

Create a new Buildx builder instance and set it as default:

docker buildx create --name achrafbuilder --use
docker buildx inspect --bootstrap

Step 5: Build and Push the Image with Cache Export

Now, let’s measure the build time using BuildKit and export the cache to Docker Hub:

time docker buildx build --platform linux/amd64,linux/arm64 \
-t hrefnhaila/myapp:latest \
--cache-to=type=registry,ref=hrefnhaila/myapp:buildcache,mode=max \
--push .

Example Output:

real    1m30.646s
user 0m0.299s
sys 0m0.089s

Step 6: Import Cache in Subsequent Builds

For subsequent builds, import the cache to further reduce build times:

time docker buildx build --platform linux/amd64,linux/arm64 \
-t hrefnhaila/myapp:latest \
--cache-from=type=registry,ref=hrefnhaila/myapp:buildcache \
--cache-to=type=registry,ref=hrefnhaila/myapp:buildcache,mode=max \
--push .

Example Output:

[+] Building 9.4s (17/17) FINISHED
...
real 0m9.646s
user 0m0.299s
sys 0m0.089s

Results and Comparison

By comparing the output of the time command, you can see the difference in build times. Typically, builds using BuildKit and cached layers will be significantly faster than traditional builds without caching.

Speed Up you’re CI/CD Pipelines Using Remote Cache (Buildkit)

The caching mechanism provided by BuildKit and Buildx is particularly useful in CI/CD pipelines.
By exporting the build cache to a Docker registry and importing it in subsequent builds, you can significantly speed up your build times across different environments and machines.

  1. Export Cache After Initial Build:

docker buildx build --platform linux/amd64,linux/arm64 \ -t hrefnhaila/myapp:latest \ --cache-to=type=registry,ref=hrefnhaila/myapp:buildcache,mode=max \ --push .

Import Cache in Pipeline Builds:

In your CI/CD pipeline configuration, you can include the cache import to leverage the previously stored cache:

docker buildx build --platform linux/amd64,linux/arm64 \ -t hrefnhaila/myapp:latest \ --cache-from=type=registry,ref=hrefnhaila/myapp:buildcache \ --cache-to=type=registry,ref=hrefnhaila/myapp:buildcache,mode=max \ --push .

By doing this, the CI/CD pipeline can use the cache stored in the Docker registry, resulting in faster builds.

Conclusion

Using BuildKit and Buildx can speed up your Docker builds, making your development process more efficient. you can save valuable time and resources. Start using BuildKit and Buildx today to optimize your Docker image builds!

I hope this will help ,If you have any further questions or run into issues, don’t hesitate to reach out.

NHAILA Achraf

#dockerbuild #buildkit #buildx #devops #docker #pipeline #azure #cicd #cache

--

--