Balena build locally pull cache

Hello, is there an option to pull cache during balena build --emulated?
Like pulling latest image that were build
I can’t find it anywhere

Hey there,

The balena build command has a --cache-from flag. Is this what you are looking for? You can read more about it here

Hey
But i need image name in order to use cache from
Can i read image name from release somehow? What about if i have multiple containers

Hi, you are correct. The cache-from option for balena CLI works the same as the docker option, i.e., If the required images are not found locally it will look for them on the docker registry. For now there is no way to pull images pushed to the balena registry to use as cache for local builds. However, the CLI doesn’t remove local images after a deploy so subsequent builds should be much faster.

Does that answer your question? It’s a good idea to allow using registry stored images as local build cache, I’ll pass that along to our CLI team.

Also, is there any reason you default to using emulated builds instead of building on a local device or using balena push? What kind of device are you using?

Subsequents builds are ok, but then we can’t distribute jobs on the workers to pull cache
For now our current workaround is to make base images with all dependencies compiled in

It at least shows me that i didnt missed anything

The reason is that im using open-balena :slight_smile:

Hey, some more things you could try to enable caching on your distributed workers by using a local registry or private dockerhub or local file server.

local registry or dockerhub

  1. docker pull my.local.registry/myorg/myapp:latest || true
  2. balena build --cache-from my.registry/myorg/myapp:latest
  3. balena deploy ...
  4. docker tag <image-from-above> my.registry/myorg/myapp:latest
  5. docker push my.registry/myorg/myapp:latest

local file server

  1. cp local/file/server/image.tar .
  2. docker load -i image.tar
  3. docker tag <sha256-from-above> myapp:latest
  4. balena build --cache-from myapp:latest
  5. docker save <image-from-above> -o image.tar
  6. cp image.tar local/file/server/

Essentially after a successful build on a worker, you could tag the image and either push it to a non-balena registry, or save it in a tar on a fileshare to be loaded by the next worker.

Uhh then i need to host my own registry, when its already hosted on open-balena side, so that’s doubling resources + complicating things, where the ideal solution is to just use existing one

For now we are using workarounds like building base-images, but in long term it makes us additional time waste

I understand that running an additional registry or file server is not ideal, I just wanted to lay out the options I was aware of.

Unfortunately our push command does not store images in a manner with predicable tags, so pulling a previous image requires using the API to determine the full path to the image.
There are some curl functions you could try with the API to get the full registry path to the image, then you could docker pull and use the cache-from flag when building.

Here’s an example function to get you started, but you’ll find it depends on other functions in the same file:

Oh, that’s very very helpful. Thanks in advance!

Let us know how querying the API works out for you!

It would be cool if balena-cli would support this feature. With multistage builds there is no possibility to take the cache from a registry, because the previous stage is not pushed.