Creation Of The Hammurabi Game Part 2 - Run The App In A Docker On The RaspberryPi

Hello,

In this documentation I want to show, how I have set up a Java Springboot Application in a docker container, that runs on a Raspberry Pi.

In the long run I want to create a simple game with the name "Hammurabi", which uses hive content to influence the run of play.

Precondition:

Java Springboot is a base (Hello world) package, that can be easily created via https://start.spring.io/.

Here is a description, how I have set up a Java Spring Boot Server: https://peakd.com/hive-169321/@achimmertens/creation-of-the-hammurabi-game-part-1-spring-boot-server-from-scratch
It runs in my IntelliJ IDE, but can also run just with "java -jar xxx.jar"

Create A New Jar File

Now, that I have some Java code, I want to export my app into a jar file. For this I click in the right upper corner on "Maven/Hammurabi/Lifecycle/package/run Maven Build"

image.png

This creates a jar file in the target folder:

image.png

I have copied it to my RaspberryPi:

scp C:\Users\User\git\Hammurabi\backend\target\hammurabi-0.0.1-SNAPSHOT.jar pi@raspi:~/git/Hammurabi/backend/Docker

image.png

There I have created a file called "Dockerfile" with the following content:

pi@raspberrypi:~/git/Hammurabi/backend/Docker $ cat Dockerfile
# Build time
FROM arm32v7/openjdk
WORKDIR /
ADD hammurabi-0.0.1-SNAPSHOT.jar app.jar

#Run-Time
EXPOSE 8080
CMD java -jar app.jar

The trick is, to find the right base image, that contains a minimal Operation System. In my case for my Raspberry Pi I had to take "arm32v7/openjdk". For other images watch out here.

Building The Docker Image

Now, with these both files in a folder, I have build my docker image:

docker build --tag hamm01.img .

image.png

An image is like the content of an CD (Compact Disk, for the elder public ;-)). It is just dumb code and cannot be written.
A docker container is a runtime environment (kind of tiny virtual machine), that launches the code. I can start it with:

docker run -p8080:8080 --name hammurabi hamm01.img

(Or if we want to run it in the background:
docker run -d -p8080:8080 --name hammurabi hamm01.img
)

image.png
Now I can connect with the browser to the RaspberryPi http://raspi:8080

image.png

It works!

How to stop and redeploy a container and image

This shows how we can stop the container:

image.png

And with these commands we remove the image. This is necessary to build (deploy) a newer image of the container. First we stop and delete the container:

  • docker ps -a -> shows all containers (including the not running containers)
  • docker stop -> stops the running container
  • docker rm -> removes the container

Now we delete the image:

  • docker images -> shows which images are installed
  • docker rmi -> removes that image

image.png

If we want to redeploy an image, now we can go back to "Create A New Jar File"


In my next documentation I show how I have created some Java code to enable web-requests via RestAPI, so stay tuned.

Regards, Achim Mertens

Sort:  


The rewards earned on this comment will go directly to the people sharing the post on Twitter as long as they are registered with @poshtoken. Sign up at https://hiveposh.com.

pixresteemer_incognito_angel_mini.png
Bang, I did it again... I just rehived your post!
Week 109 of my contest just started...you can now check the winners of the previous week!
!PIZZA
6

PIZZA!

PIZZA Holders sent $PIZZA tips in this post's comments:
@pixresteemer(5/5) tipped @achimmertens (x1)

Join us in Discord!