pants packaging a docker image using a kubernetes buildx driver (and no docker daemon) #22520
gdfast
started this conversation in
Tips and Tricks
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
These are lessons from troubleshooting the issue discussed here: https://pantsbuild.slack.com/archives/C046T6T9U/p1751926012583459
tl;dr
To use a kubernetes buildx driver
KUBERNETES_SERVICE_[HOST|PORT|PORT_HTTPS]env_vars to the [docker] backend config of pants.tomlBUILDX_BUILDER=<your k8s buildx driver name>,BUILDKIT_NAMESPACE=<your namespace>docker_imagetarget, setoutput={"type": "registry"}pants publishon that target becausepants packagewill upload to the registryPremise
The ability of pants to optimally build docker images with pex files is pretty compelling (see this blog). This is made even better with buildx support in the pants docker backend, which enables multi-platform docker image builds.
Normally, with docker desktop installed,
pants packageandpants publishof a docker_image target using buildx "just works".packagewill build the image and store it locally,publishcan push it up to a registry.However, I recently hit an issue moving the pants commands that worked on my laptop to a (Gitlab) CI job had some tricky failures to overcome.
More about the CI environment
For our CI jobs, our workers run a Docker image that has the docker CLI installed. There have long been security concerns and complications running docker in docker (dind). For that reason, the team in charge of CI infrastructure at my company does not have a docker daemon running on the CI container (no socket). Instead, there's a kubernetes cluster of BuildKit workers available for building the images triggered by a
docker buildx buildcommand.On my laptop, I have the following build backend
In CI, we instead have
Notice "default" status is "error" and that last line... there's no default docker daemon
Overcoming
pants packagefailure 1 – "cannot determine Kubernetes namespace"On buildx version v0.19.2 I got
And on the older buildx v0.11.1 I got
There's no kubeconfig file because the CI worker pod is in-cluster, which means it should get its namespace and cluster information from the kubernetes server. To get this to work for
docker buildx buildlaunched by pants, I had to pass theKUBERNETES_SERVICE_HOSTandKUBERNETES_SERVICE_PORTenvironment vars to the docker backend's env_vars. With that info exposed, thedocker buildx buildexecuted by pants was able to pull the info it needed to use thecibuildx kubernetes driver.Overcoming
pants packagefailure 2 - "failed to copy to tar"Next,
pants package <docker_image target>almost succeeded in building the image, but failed with the following error:Inspecting with
pants -ldebug, we see that pants is running the following commandtype=dockercannot work as an output type for remote docker builds when there's no local docker engine running that can save the image. The solution was to set--output=type=registryon the docker_image.❗ This means there's no need for a
pants publishstep:pants publishpushes up an image from the local engine to a registry, but the output ofpants packageon the docker_image target already was that registry.Putting it all together (working in pants v2.27.0)
pants.[ci.]toml
BUILD target
Beta Was this translation helpful? Give feedback.
All reactions