Kuby-core: Using buildx

Created on 13 Dec 2021  ·  6Comments  ·  Source: getkuby/kuby-core

I was trying to add support for invoking docker build as docker buildx build but I'm afraid it might be a little more complicated than that

My intention is to add these type of params:

https://github.com/fluxcd/image-reflector-controller/blob/62c06ea58cd14072fde2c9ada7c6970dedf580e5/.github/workflows/build.yaml#L57-L59

Then we can cache Docker build layers in a more efficient way. Anyway the --cache-to and --cache-from options are only available in Docker Buildx, like a lot of other options that I'd maybe like to use

In the past I've successfully enabled cache volumes for bundler inside of a Dockerfile, so that changes to the Gemfile and Gemfile.lock would not result in a complete recalculation of the image's build-time dependencies, but would instead build from cache so that only new dependencies were recalculated.

While this is obviously a niche optimization, there are other more common use cases that can only be handled well with buildx. The buildkit toolset is becoming pretty standard now IMHO. I'd love it if it were possible to invoke Kuby build in a way that uses buildx!

I might try implementing this again later, it might be easier now that 0.15.0 is solidly released. In the meanwhile, there is a functioning example of GitHub Actions that is WIP but currently looking pretty good at: https://github.com/kingdonb/kuby_test

With caching properly configured in this one last place, and a couple of other changes, I think this is nearly ready to tag and make an example out of it for contribution back to kuby-core, or to live somewhere in the getkuby project 👍

Most helpful comment

Alright, if docker build is just an alias for docker buildx, then I don't see a need for Kuby to do anything special. It should be up to the user to set up the builder they want.

All 6 comments

Hey!

It's possible to use buildx right now, if you set it as a default builder.

Here is an example Github Action which uses buildx and its caching capabilities: https://github.com/anycable/anycable_rails_demo/pull/25/files#diff-28802fbf11c83a2eee09623fb192785e7ca92a3f40602a517c011b947a1822d3R28-R44

See also https://evilmartians.com/chronicles/kubing-rails-stressless-kubernetes-deployments-with-kuby#deploying-continuously-with-kuby-and-github-actions

I was just looking for something like the --only flag, as I was having trouble setting up caching! I got the idea that I needed to separate out scopes for each build type, like I see you have done here.

Thanks so much for the handy references 👍 I'm following along now to see if they get me across the line!

I was definitely also missing the default builder feature of setup-buildx-action (install: true)

It seems to be working now! I just saw an app image build complete in less than 10 seconds. There are other problems I'm having now, possibly a regression that I found where RAILS_MASTER_KEY is not being exported properly, but I'm going to go and read the source to find out now.

If I use the install: true like in this commit, then the build method from lib/kuby/docker/cli.rb invokes buildx, and I can pass in buildx constructions like --cache-to after the -- to get them in the *docker-args array 👍 so this can be closed, except that it might be nice for kuby-core to document how it can be used with buildx, it's strictly not kuby's problem

With the example in:

https://github.com/kingdonb/kuby_test/commit/d74bf51e04253359b0d54aae7ddd6723ea6fd41d#diff-e2ad06bfd6b3aefc2d5f4fe8f60e0015b16947abc16764adef02e4339ac07a42R17-R20

I can use buildx, so this issue can be closed.

This is really cool, thanks for figuring the buildx stuff out! I was under the impression you could set an environment variable and it would Just Work™, is that what that docker/[email protected] action is doing? It seems that buildx comes with Docker Desktop and newer versions installed via deb/rpm packages, so I wonder if Kuby could simply default to using it. If it's configurable via env var though, maybe invoking docker buildx build isn't necessary? Maybe Kuby can detect it and fall back to normal docker build?

Yes, it's possible (and that's what install: true in the GitHub Action does): https://docs.docker.com/buildx/working-with-buildx/#set-buildx-as-the-default-builder

Alright, if docker build is just an alias for docker buildx, then I don't see a need for Kuby to do anything special. It should be up to the user to set up the builder they want.

Was this page helpful?
0 / 5 - 0 ratings