Spring Boot REST app in a Docker Container on the Raspberry Pi

Docker on the Raspberry Pi? Sure, with help from Hypriot who provide an iso image ready to go, getting docker up and running on the Pi is as simple as burning their image to an sd card and booting it up. Ok, so that part is simple.

Building an image to run on the Pi is not as straightforward as it sounds. Well, it’s no more complicated than building an image from a Dockerfile, but given that the Pi is ARM based and not x86, you have to build an image that is built/compiled with executables/libraries for ARM.

This makes complete sense when you think about it, you’re not going to get an image containing x86 executables and libraries to run on a Pi, but it’s rather sad that it isn’t possible to build an image for example from my Mac and and I can run on my Mac, or Linux, or on the Pi. Well, you can build the image on any architecture, but you can’t run an ARM compatible image on the Mac (or docker-machine/boot2docker).

Ok, so that in mind, there are a number of ARM compatible images on Docker Hub already – Hypriot also have base images ready to go. So to run Spring Boot in a container on the Pi, I need an ARM image with Java. Luckily, here’s one ready to go.

For a quick test, I took my simple Sprig Boot REST app I already have on GitHub, and threw together a Dockerfile using Alpine first to check it would run locally:

FROM java:openjdk-8-alpine

ADD SpringBootREST-0.0.1-SNAPSHOT.jar /opt/SpringBootREST-0.0.1-SNAPSHOT.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/opt/SpringBootREST-0.0.1-SNAPSHOT.jar"]

This runs as expected in docker-machine on my Mac.

To push to the Pi, the only change is to replace the base image with an ARM compatible image with Java, so this looks like:

FROM hypriot/rpi-java

ADD SpringBootREST-0.0.1-SNAPSHOT.jar /opt/SpringBootREST-0.0.1-SNAPSHOT.jar

EXPOSE 8080

ENTRYPOINT ["java", "-jar", "/opt/SpringBootREST-0.0.1-SNAPSHOT.jar"]

To get it to my Pi, I set up a local Registry to push the image to, and then pulled it from the Pi. So tag the image to the registry:

docker tag spring-boot-rest-rpi registry-ip:5000/spring-boot-rest-rpi

…push to the registry:

docker push registry-ip:5000/spring-boot-rest-rpi

and then pull from the Pi:

docker pull registry-ip:5000/spring-boot-rest-rpi

And then fire it up!

docker run -it -p 8080:8080 registry-ip:5000/spring-boot-rest-rpi

Spring Boot in a Docker Container on the Pi!

6 Replies to “Spring Boot REST app in a Docker Container on the Raspberry Pi”

  1. I did the same but it’s very long to start (when using docker run ..)on my rpi3.

    some few lines:
    2017-08-27 13:21:54.021 INFO 7 — [ main] com.example.demo.DemoApplication : No active profile set, falling back to default profiles: default
    2017-08-27 13:21:55.403 INFO 7 — [ main] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@4f6be2: startup date [Sun Aug 27 13:21:55 UTC 2017]; parent: org.springframework.context.annotation.AnnotationConfigApplicationContext@b37bc
    2017-08-27 13:24:35.499 INFO 7 — [ main] o.s.cloud.context.scope.GenericScope : BeanFactory id=2e19ee11-0127-3466-87da-01e6c503c313
    2017-08-27 13:24:40.329 INFO 7 — [ main] f.a.AutowiredAnnotationBeanPostProcessor : JSR-330 ‘javax.inject.Inject’ annotation found and supported for autowiring

    have you an hint in order to accelerate the start?

  2. I’ve hade a really slow startup with the “hypriot/rpi-java”. I changed to “jsurf/rpi-java” and starts faster.

    But:
    jsurf/rpi-java ~ 313 Mb.
    hypriot/rpi-java ~ 225 Mb.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.