Home > Mobile >  "Mount" a Docker buildx builder from host to container
"Mount" a Docker buildx builder from host to container

Time:07-24

(How) can I "mount" a Docker buildx builder from a host to a container?

I have a Docker buildx builder foo on a host system (CentOS Stream 8).

[root@myhost ~]# docker buildx inspect --bootstrap
[ ] Building 1.4s (1/1) FINISHED                                                                                                                                      
 => [internal] booting buildkit                                                                                                                                  1.4s
 => => starting container buildx_buildkit_foo0                                                                                                                 1.4s
Name:   foo
Driver: docker-container

Nodes:
Name:    foo0
Endpoint:  unix:///var/run/docker.sock
Status:    running
Platforms: linux/amd64, linux/amd64/v2, linux/arm64, linux/riscv64, linux/ppc64le, linux/s390x, linux/386, linux/mips64le, linux/mips64, linux/arm/v7, linux/arm/v6

How can I make this builder available to a Docker container that I start on the host?

Context

I want to build multi-arch images in GitLab CI containers. That works just fine already but only if I create the buildx builder ad-hoc inside the container i.e. docker buildx create --use. This throw-away builder dies if the CI container dies when the job it executes is finished.

The problem is that the Docker build cache is tied to the builder. Hence, the ad-hoc builder inside the container always starts with an empty cache. If I could use the buildx builder from the host inside the container I get an effective cache.

CodePudding user response:

It appears to be sufficient to mount the .docker/buildx directory into the container. buildx operations inside the container will then use the builder cache of the host.

Example

docker run --rm -ti -v /home/myuser/.docker/buildx:/root/.docker/buildx -v /var/run/docker.sock:/var/run/docker.sock docker sh

Inside the container you can use the builder foo defined on the host in two ways:

  • docker buildx use foo to set it as the default builder
  • docker buildx build --builder foo ... to explicitly set it for a single operation
  • Related