How to distribute code to run on different architectures? Answer: Docker buildx

How to distribute code to run on different architectures? Answer: Docker buildx

Intro

In case you don't know Docker... How did you get here?

If you don't know before we begin, I highly recommend reading/watching/listening to the following contents and before continuing reading:

Give it a try and make a minimum viable product (MVP) of something in Docker -- I will still publish some cool examples of deploying web apps running Shiny and data analysis with R.

Now, if you already know Docker and have done something with it, we can talk about buildx; but first, I would like to put some scenarios:

  1. "You have an old computer, an Intel 32bits, and would like to use it as a server in your home to be able to run some of your applications. The downside is that you would like to develop from anywhere at any time."
  2. "You have a Raspberry Pi and would like to put it to good use."
  3. "Knowing that cloud services have started to feature services with ARM processors, which have lower costs, you decide to want to reduce costs of your company. "

Considering that the last two scenarios have to do with the ARM architecture -- which I intend to write more and more about here -- they may seem the same thing, but a closer inspection reveals different considerations. But even a new scenario may help you better understand the potential:

  1. "Are you tired of every 10-15 Apple changing architecture and fearing a new future they decide to switch to RISC-V or something?"

Okay, this last one is more of a guess rather than something right, or that has some current basis. But it would be easy to inject a QEMU with such architecture and, overnight day, having all of your application running on another architecture - with small to almost no changes depending on the scenario.

The buildx

The buildx in simple terms: "is that a hardware abstraction layer for Docker to build an image of one architecture in another."

41912416812_95f0353052_o.jpg

You can see, in the life cycle of a product, buildx as a private pension: you do it today but thinking about the future.

How to configure it on your machine

If you wanted a script to start developing for multiple architectures, I recommend this from here that I ended up assembling for a few months.

If you are afraid that your build time will start to take too long or consume too much energy, remember that even with the support to compile for many PC architectures, you choose the ones you want. Apart from that, I will always recommend looking for a docker build --help to see you can use more colors of your machine if you have that possibility. Remember: "Docker is your friend".

How to run now?

27870956405_1dca244cda_o.jpg

docker buildx build --platform linux/amd64,linux/arm64,linux/arm/v7 --push --tag <yourUserNoNoRepository>/<yourProject>.

That is a simple example of a command that I run for some of the personal and business applications I maintain. However, it is relevant to point out another advantage of buildx: command agglutination. That's because the old docker tag anddocker push can now be passed as flags and making a command three steps of development to be called with just one terminal command.

Some other platforms currently supported are:

  • linux/arm/v6
  • linux/386
  • linux/ppc64le
  • linux/s390x
  • and many others

How do I know if my application will work on something other than my x86?

49816253648_cc4027a918_o.jpg

The big question, but it also has the worst answer of all ... "It depends"

Currently, I have no problems compiling for ARM because I usually use images that have good support for languages ​​like Node and Python. However, if you want to go further, in addition to a stack and move up to more complex solutions, I recommend taking a look at Alpine to build your images.

Example applications

These days I develop and run three of my applications -- bots for Telegram -- in this way. I've decided to stop paying the US$: 5 per application -- which would give approximately 80 BRL -- per month to run on my Single Board Computer (SBC), which is running the three services and a lot more, all connected to a cell phone charger. If you want to see more about them:

Now, what can you contribute and run further thanks to tools like buildx?

Benefits

Having your cloud and the guarantee that your data is yours goes far beyond any LGPD, which is the Brazilian version of GDPR. Also be able to block advertisements and tracking is something that goes beyond what a lot of law can do or has already done for you.

327609942_a804d55008_o.jpg

Important points

If you like challenges and break your head at many points, try to compile all your applications to support all architectures, even if you don't use it. Your work can help others with just one script. And the more technology becomes accessible, the greater the potential for it to make a difference in the lives of other people who, until then, have not had easy access to it.

References