Improving Docker Build Speed on Raspberry Pi


overclocking raspberry pi docker airflow brute force

In my last post I wrote about running Airflow in a Docker container on a Raspberry Pi.

In the post I was able to cut my build times by installing pandas and numpy from apt instead of pip. I was happy with myself for this clever slight-of-hand, but wondered if I could do any better. After all, what is one to wonder about in times of global crisis 😳?

For fun I tried a few different approaches to cutting the image build time. None of the approaches were earth shattering but I cut my build time by 41%. While the changes I made weren’t that sophisticated and could be considered brutish, it’s still interesting to compare them side-by-side.


Optimizing the build process

Originally I was building the Airflow image on a standard RPi 3 Model B V1.2 which took 18m17s. Subbing in its cousin, the RPi 3 Model B+, improved that to 17m26s (4.6%). Not bad for simply swapping hardware.

The B+ runs at 1.4GHz compared to the 1.2GHz of the B which got me thinking about overclocking. I overclocked the B+ and shaved a little more from the build time. It dropped to 16m32s for an improvement from the baseline of 9.55%. Not bad again!

For a really cool example of clocks check out this video.

On further reading I learned about https://www.piwheels.org/. Piwheels is “a Python package repository providing Arm platform wheels (pre-compiled binary Python packages) specifically for the Raspberry Pi, making pip installations much faster”. Sounds great 🤩, exactly what I want.

To start I used piwheels for packages where possible but still installed pandas and numpy via apt. With the Pi still overclocked that brought build time down to 12m25s (32% improvement). Installing pandas and numpy using piwheels seemed to increase the build time slightly. I’m not sure why that is, but it could be due the different versions of the packages between apt and piwheels?¿

Finally I used Ethernet instead of WiFi to improve download speed and thereby build time. The less time spent downloading the better! The net/net of this was a 41% improvement in build time by using


Results

Below are the results of my tests, which are by no means scientific or definitive. They can, however, serve as a benchmark for how a bit of brute force and slights-of-hand can improve Docker build times on a Raspberry Pi.

The same SD card was used for all tests, and I monitored the temperature & CPU to check for throttling. In all cases where I used a heat sink and a fan I was nowhere close to throttling, so I’m not concerned about the missing temps for two of the tests (I forgot to run the monitoring script during the build process 🤷🏼‍♂️).

TestModelBuild TimeNetwork Interfacepywheelscpu_freqOver Voltagegpu_freqMax TempAvg TempHeat sink
1RPi 3 Model B V1.218m17.626swlan0False1.2GHz
2RPi 3 Model B+17m26.790swlan0False1.4GHz56.9’C54.07’CYes
3RPi 3 Model B+16m59.547swlan0False1.55GHz635.9’C34.6’CYes + Fan
4RPi 3 Model B+16m32.816swlan0False1.55GHz6500MHz35.9’C34.6’CYes + Fan
5RPi 3 Model B+12m25.836swlan0True (apt for numpy/pandas)1.55GHz6500MHzYes + Fan
6RPi 3 Model B+14m51.080swlan0True1.55GHz6500MHz38.1’C36.1’CYes + Fan
7RPi 3 Model B+13m48.656seth0True1.55GHz6500MHz33.2’C32.1’CYes + Fan
8RPi 3 Model B+10m50.112seth0True (apt for numpy/pandas)1.55GHz6500MHz33.2’C32.5’CYes + Fan

It’s interesting to note that turning WiFi off also seems to have dropped the temp a few degrees, from 38.1’C to 33.2’C, between tests 6 and 7.


Final Words

I didn’t have a spare Raspberry Pi Model 4 laying around, but did happen to order one last week from Adafruit. Once that arrives I’ll be curious to how it performs with the same test.

Lastly…

Overclocking happens in the /boot/config.txt file by adding these lines

arm_freq=1550
over_voltage=6
gpu_freq=500
Overclocking can lead to bricked Pi’s and voided warranties 😈. Make sure you adequately cool the Pi with a heatsink and/or fan to avoid throttling.

Here’s what the final Dockerfile looked like, with the overall diff from the tutorial being ENV PIP_EXTRA_INDEX_URL=https://www.piwheels.org/simple.