Build Docker images on k8s nodes
In one of my previous posts, cross-compiling for Raspberry Pi with Docker, I wrote about and illustrated how the relatively new buildx command made it much easier to build and dump binaries on the host system.
Another useful feature of buildx is the ability to use different drivers when building images. As of today (10.10.2020), it supports 3 different drivers:
docker
- uses the docker daemon built in builder (default). You’d use this if you want to build and use images locally.docker-container
- uses a BuildKit container spawned by docker. Useful when you want to build multi-platform images.kubernetes
- uses k8s pods with defined buildkit container images. Similar to thedocker-container
driver, but instead of building on the host, it builds on a k8s node
I find the kubernetes
driver quite useful when you’re low on resources on your host or when you just want your builds to be fast; there’s probably other good reasons to use it (scalability?), but I’m not going to try to convince you. You should just try it and see if it fits your needs or not.
And the setup doesn’t take more than a minute, definitely less than the time it takes to read this post.
I’m going to assume you’ve already created a K8s cluster on GCP or AWS (or some other cloud provider), but to make sure, check that the current context is what you expect:
|
|
Should, on a GKE cluster, show something similar to:
|
|
Now, create a builder:
|
|
You can provide other options too; let’s say you want a couple of replicas:
|
|
--use
flag will switch to that builder automatically.And bootstrap it:
|
|
That’s it, you’re ready to build images on your k8s nodes.
If you find this post helpful, please consider sponsoring.
Sponsor