Build Docker images on k8s nodes
Docker Buildx allows you to route image build execution away from your local workstation using pluggable builder drivers:
docker: Uses the standard daemon engine (default). Best for single-arch, local image generation.docker-container: Spawns an isolated BuildKit container locally. Supports multi-platform builds and cache export.kubernetes: Deploys BuildKit pods dynamically into a Kubernetes cluster, offloading compilation, packaging, and disk I/O to remote node pools.remote: Directly connects to a standalone BuildKit daemon over mTLS or TCP, useful for dedicated shared build infrastructure.
The kubernetes driver is particularly effective when local CPU and RAM are constrained, or when you want your build tasks to scale horizontally across high-performance cloud compute.
Prerequisites
Verify your kubectl context points to the target cluster:
kubectl config current-contextCreating the Kubernetes Builder
Create a new Buildx builder using the kubernetes driver:
docker buildx create \
--name k8s-builder \
--driver kubernetes \
--driver-opt namespace=buildkit,replicas=2 \
--driver-opt requests.cpu=2,requests.memory=4Gi \
--driver-opt limits.cpu=4,limits.memory=8Gi \
--use--use flag automatically switches your current Buildx context to this new builder.Bootstrap the builder instance to pull the BuildKit image and launch the Kubernetes pods:
docker buildx inspect --bootstrapCheck that the builder pods are running in your cluster:
kubectl get pods -n buildkit -l app=buildkitRunning Builds with Remote Caching
Once initialized, run docker buildx build as usual. Because the builder lives inside the cluster, push outputs to a container registry or export them locally:
# Push directly to registry with inline cache
docker buildx build \
--tag my-registry.example.com/app:v1 \
--push \
--cache-to type=registry,ref=my-registry.example.com/app:buildcache,mode=max \
--cache-from type=registry,ref=my-registry.example.com/app:buildcache \
.Or export the resulting filesystem directly back to your local machine:
docker buildx build -o type=local,dest=./dist .Cleanup
To remove the builder and its Kubernetes resources:
docker buildx rm k8s-builder