72

I tried export DOCKER_BUILDKIT=1 before a docker-compose build command and I did not see the expected BuildKit output. What did I miss?

Remolten
  • 2,614
  • 2
  • 25
  • 29
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

3 Answers3

106

Support for BuildKit was just released in docker-compose 1.25.0. To enable:

export DOCKER_BUILDKIT=1 # or configure in daemon.json
export COMPOSE_DOCKER_CLI_BUILD=1

With those variables set in your shell, you can now run docker-compose build using BuildKit.

In windows you can execute in your console:

setx DOCKER_BUILDKIT 1 # or configure in daemon.json
setx COMPOSE_DOCKER_CLI_BUILD 1

after will need restart your console

buster95
  • 640
  • 7
  • 7
BMitch
  • 231,797
  • 42
  • 475
  • 450
  • 2
    Is this still the right answer? How does this play with `docker compose` vs `docker-compose`? – Andy Ray Sep 15 '21 at 17:35
  • 1
    This does not work when using sudo. How can I set COMPOSE_DOCKER_CLI_BUILD globally instead of an environment variable? – Neal Oct 24 '21 at 01:24
  • @AndyRay iirc, `docker compose`(without the hyphen) is a sort of a plugin that docker made to tackle "how to deploy structure prepped by docker-compose file on AWS's ECS", as opposed to Amazon's take on that is "docker-compose is an implementation detail for development environment, which we are not going to cater to. We have ECS CLI (or better yet, the latest Copilot)". – Hiro Apr 08 '22 at 20:31
  • 3
    This is very inconvenient, it's a pity that we can't manage it via yml file – Alexander Goryushkin Apr 29 '22 at 06:33
32

You can use this command to tell docker-compose to use the Docker CLI when executing a build.

COMPOSE_DOCKER_CLI_BUILD=1 docker-compose build

You should see the same build as usual, but with this warning:

WARNING: Native build is an experimental feature and could change at any time

And you can go like that to parametrize the CLI to use BuildKit instead of the default builder:

COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build

Windows version:

set "COMPOSE_DOCKER_CLI_BUILD=1" & set "DOCKER_BUILDKIT=1" & docker-compose build

You can also enable BuildKit globally, editing /etc/docker/daemon.json file, adding:

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

For more informations: https://docs.docker.com/develop/develop-images/build_enhancements/

veben
  • 19,637
  • 14
  • 60
  • 80
7

Docker Compose v2 supports BuildKit by default: link

While most migrations should be straightforward, Compose v2 does introduce a few breaking changes which could impact specific use cases:

  • Containers are now created with hyphens in their names instead of underscores.
  • docker compose build builds with BuildKit by default.
  • Some deprecated command flags have been removed.
Kye
  • 4,279
  • 3
  • 21
  • 49