Balena Base Images with balena-sdk Installed?

Hello! I have an application where I am using the balena-sdk on the balena device. Whew installing that package is quite a pain though. It requires rust and all kinds of things which really slows down the build time of the docker image.

Is there a balena base image that already includes the balena-sdk? Right now I’m starting with FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3.9

Hi,

Have you considered using multi-stage builds?
This can help speed up things by caching the image with SDK installed and then only rebuilding your application image.
Something like this:

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3.9 as builder
RUN install_packages <balena_sdk and other big things>

FROM builder as app
RUN install_packages <custom packages>

CMD [ <your app> ]

For the first time building, this shouldn’t make much of a difference, but subsequent builds can grab the builder image from cache.
See also here for an example of only using part of the builder image.

1 Like

Great suggestion from @tjvv, multi-stage builds would be the right way to go about this. I assume, you are installing the Python balena-sdk. Can you share your dockerfile? I don’t remember having the need for rust or other things to install balena-sdk from pip.

Sure thing! The dockerfile in question is here.

You can see in the contents below that I was actually able to skip a rust installation by using the ENV variable CRYPTOGRAPHY_DONT_BUILD_RUST=1. Apparently the rust compiler is needed for the cryptography library (required by balena-sdk) to build the library for some 32 bit arm platforms.

Perhaps a multistage build would be a good solution, but it could be nice for their to be officially supported images that contain the SDK from the Balena team as well (what I was hoping existed when I asked the question :slight_smile: )

FROM balenalib/%%BALENA_MACHINE_NAME%%-python:3.9

RUN install_packages gpsd gpsd-clients dbus build-essential libffi-dev python-dev libssl-dev
RUN pip install wheel

ENV CRYPTOGRAPHY_DONT_BUILD_RUST=1
RUN pip install cryptography==3.4.6
RUN pip install balena-sdk

WORKDIR /usr/src
COPY ./gpsd.py /usr/src/gpsd.py

CMD python gpsd.py

The dockerfile you shared could benefit massively from the multistage build that @TJvV suggests mostly in terms of its size. Would really recommend it. Build-essentials for example doesn’t need to stay and makes for more for users to download.

For pip packages like this check out this: Multi-stage builds #2: Python specifics. I use the —user option and works great, very clean and produces small images.

Could also switch to an alpine image assuming you can still keep all your images consistent.

And could check out the -build version of balena images which have a bunch of that build stuff pre installed so you could skip a few steps. Then copy it across to the smaller image at the last stage of the multi build.

Thanks for all the info! Yes I will look into the multistage builds.

I guess I was surprised to see that the balena-sdk wasn’t included in the build image variants. Given that these images are designed to go on Balena devices, it seems like it would make sense to include it there?

Isn’t it for working off of a device, for websites or desktop apps? It uses a public api over the internet? What is it you are trying to do with it?

We use it to dynamically set the dt_overlay of the device based on different hardware configurations. You can see what we’re doing at this link for example:

I suspect you can communicate directly to the hardware. Interact with hardware - Balena Documentation

Hopefully Balena people can be more helpful.

Thanks for the thoughts Maggie! Unfortunately editing the dt_overlay isn’t possible without the Balena SDK but hopefully the Balena staff can weigh in if that’s truly the case or not.

Hey @maggie0002 and @keenanjohnson! We don’t currently ship the SDK in our base images in order to keep the size down, but it can be easily installed via npm if needed. I still made note of your request in case more users ask for the same thing!

However, if you want to skip the SDK altogether you can contact the API directly with curl or similar. Here are the endpoints you would be looking for to set device config variables like BALENA_HOST_CONFIG_dtoverlay.

Let us know if there’s anything else you need!