1. Overview

Docker Compose is an automation tool that makes it easy to manage multi-container applications. BuildKit is a powerful build engine within Docker that improves the performance and flexibility of our application builds. In addition, it helps to create effective build pipelines and integrates seamlessly with Docker images and Docker Compose files.

In this tutorial, we’ll explain how to enable BuildKit with Docker Compose.

2. Understanding the BuildKit

BuildKit is a feature included with the Docker daemon that simplifies the process of building and packaging container images. With BuildKit, we can run parallel, incremental, and multiple-stage builds in Docker. As part of BuildKit, we can now specify build-time secrets and build-time arguments, which makes the build process more customized and optimized. It also provides a new image format to make image distribution and storage more efficient.

Compared to prior versions of Docker, this BuildKit builder is more powerful, and it offers many enhanced features and improvements over the legacy builder.

3. Enabling BuildKit with Docker Compose

To start using BuildKit in our workflow, we’ll need to have Docker version 18.09 or above. Before proceeding, we’ll need to upgrade Docker if we have an older version.

3.1. Using the Docker Daemon Config

To enable BuildKit through Docker daemon configuration, we’ll need to set the buildkit feature to true in the configuration file. As soon as the Docker daemon activates BuildKit, we can use it to build images. Let’s add the following configuration to the /etc/docker/daemon.json file to enable BuildKit:

{
  "features": {
    "buildkit": true
  }
}

Now, we need to restart the docker service:

$ sudo systemctl restart docker

To use BuildKit features, we can use the docker build command with the –progress=buildkit flag or set the DOCKER_BUILDKIT environment variable.

3.2. Using the docker-compose.yml

To enable BuildKit in Docker Compose, we need to add the DOCKER_BUILDKIT=1 ENV variable in docker-compose.yml. To illustrate, let’s create a docker-compose.yml file:

version: '3.8'
services:
  web:
    build:
      context: .
      args:
        DOCKER_BUILDKIT: 1

In this configuration, the web service uses Docker Compose version 3.8 with the environment variable DOCKER_BUILDKIT. This tells Docker Compose to use BuildKit when building the web service.

3.3. Using the export Command

We can also enable BuildKit by setting the DOCKER_BUILDKIT environment variable in our shell before running the docker-compose build command. Let’s look at the command:

$ export DOCKER_BUILDKIT=1

The above command sets the DOCKER_BUILDKIT value to 1. When building Docker images, this ENV variable enables BuildKit. When set to 1, it enables BuildKit; when set to 0 or unset, it disables BuildKit. By default, Docker disables BuildKit and uses the legacy builder instead.

We used the export command to make the environment variable available to all child processes created in the current shell session. As soon as this command is executed, all subsequent docker build commands will use BuildKit rather than the legacy build engine. Let’s look at the command to build the services defined in docker-compose.yml:

$ docker-compose build

With the above build command, images will be created for all services defined in docker-compose.yml. Moreover, it will use BuildKit as the default engine, as the ENV variable DOCKER_BUILDKIT is set to 1.

4. Advantages of BuildKit

The BuildKit engine allows us to improve the performance and flexibility of our application builds. It is designed in such a way that it works seamlessly with Docker. Also, it can be integrated easily to provide efficient and optimized build pipelines. Its multi-stage build can effortlessly reduce the overall size of our final images. This eventually helps to reduce the disk space on the server and saves time during the development process.

By using BuildKit’s features, such as multiple build stages, parallelism, and caching, we can significantly improve the performance and efficiency of our application builds. This can help us to save time and resources during the development process and ultimately lead to faster and more efficient deployments.

4.1. Multiple Build Stages

One of the key advantages of BuildKit is its support for multiple build stages. This helps us to divide the complete build process into multiple stages, where each stage performs a specific task. Using BuildKit, we can easily separate the build stages. One stage is to compile the code, and another stage is to copy the compiled code to the runtime image. In this way, our final image will be smaller and more efficient.

4.2. Parallelism and Caching

To speed up our builds, BuildKit also allows parallelism and caching. Parallelism allows us to perform multiple tasks simultaneously, which eventually increases the speed of our builds. Furthermore, it provides caching to reuse previous build results. By doing this, we can save both time and resources during the development stage.

5. Conclusion

In this article, we discussed how to enable BuildKit with Docker Compose. First, we explored different ways to enable BuildKit with the docker-compose.yml file. After that, we discussed various advantages of BuildKit to improve the development workflow.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.