How to run Spring Boot Application on Raspberry Pi Using Docker

Spring boot + Docker + Gitlab + Raspberry Pi = Success!

Antti Havanko
The Startup
3 min readAug 31, 2020

--

By frank mckenna on Unsplash

Docker is a really useful tool for running applications because it enables you to easily pack, deploy, and run any application, which can run anywhere. As Docker containers don’t have much overhead you can even run them on low-end IoT devices. Like Raspberry Pi (even though they’re quite powerful nowadays!)

Building the Docker images for Raspberry Pi is a bit different due to the ARM-based CPUs it has but luckily Docker’s new multi-arch builds makes this much easier. Let’s go through how to build a custom Docker image for Raspberry Pi and run a Spring Boot application with it.

Setting up Raspberry Pi

I’m using the great chilipie-kiosk image from Futurice. It allows booting directly into full-screen Chrome so you can easily start your web app on boot.

After you have the Raspberry Pi running chilipie-kiosk, you can go into installing Docker. Running the standard installation script will do the trick:

Building the application

You obviously need to package your application into a Docker image first. There are many ways to do it so I’m not going into details on how to do it.

Here’s a Dockerfile for an application with Spring boot backend and AngularJs front end (yeah, it’s an old app). The following multi-stage build first builds the backend & front end and then combines them into the final image:

A couple of things to notice here:

  • As you’re building for Raspberry Pi (i.e. ARMv7 architecture), you must use ARM-compatible images. Luckily many images are.
  • With multi-stage builds, also the intermediate images must be built for ARM.
  • Here we’re using ARM32 specific base image so you can’t actually run this on any other architecture.

Building multi-architecture image

Docker’s buildx functionality enables you to build Docker images for different architecture than where you’re building the image in. E.g. you can build an image for ARM32 on a machine running on AMD64. You can use either remote builders that are running on different architectures or QEMU emulator. Since you probably don’t have any build farm, let’s use the QEMU emulator.

Docker buildx is still an experimental feature so you must enable it with “DOCKER_CLI_EXPERIMENTAL” environment variable. Otherwise, you’ll see the “docker: ‘buildx’ is not a docker command.” error from Docker CLI.

Here’s a Gitlab .gitlab-ci.yml for building the multi-architecture images for the Spring boot app:

It uses Docker version 19.03.6 and first installs the latest version of buildx from its Git repository in the before_script block. Then we register the QEMU binary using the multiarch/qemu-user-static Docker image.

Next, you need to create a buildx builder for building images for the platforms you’re interested in. Here, we’re only specifying one target platform, linux/arm/v7, which is suitable for Raspberry Pi. Finally, running “docker buildx build” will build the image and push it to the Gitlab’s container registry.

Running the app on Raspberry Pi

Running the application on Raspberry Pi is as easy as running any Docker container anywhere. For pulling the images from Gitlab’s Container Registry on Raspberry Pi, you first need to create Personal Access Token and the log in as “gitlab-ci-token”.

There you go! All set! Running the application as a Docker container makes it super easy to update it as well. Just have a cron job to periodically pull the images from the registry and restart the container when there is a new version of the image.

--

--