From 6a763ace844435c076fb7a01cbb65fbdd6c61a63 Mon Sep 17 00:00:00 2001
From: Connor Holloway <c.holloway314@outlook.com>
Date: Mon, 14 Mar 2022 21:43:11 +0000
Subject: [PATCH 1/3] Support multiarch builds to run on other architectures
 e.g. Raspberry Pi

Note: This depends on the image nimmis/ubuntu being rebuilt with multiarch support

This uses docker buildx and manifest lists to build a multiple versions of an image
for different systems which appear as a single image on the docker hub.
This allows clients to select the right version automatically.

Changes:
- Select the right cpu arch version when downloading java
- Replace the java install in the Dockerfile with a call to set_java_ver
- README updated on how to build these images
---
 Dockerfile                        | 16 +++++-------
 README.md                         | 15 +++++++++++
 rootfs/usr/local/bin/set_java_ver | 43 ++++++++++++++++++++++++-------
 3 files changed, 54 insertions(+), 20 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 1be2a77..1e73cc0 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -57,19 +57,15 @@ RUN apt-get update && \
 
     # get pgp key
 
-    # create java directory
-    mkdir -p /usr/lib/jvm/ && \
-
-    # download jdk and unpack in /usr/lib/jvm
-    curl -L https://github.com/adoptium/temurin${JAVA_VERSION_MAJOR}-binaries/releases/download/jdk-${JAVA_VERSION_MAJOR}.${JAVA_VERSION_MINOR}%2B${JAVA_VERSION_UPDATE}/OpenJDK${JAVA_VERSION_MAJOR}U-jdk_x64_linux_${JAVA_OPT}_${JAVA_VERSION_MAJOR}.${JAVA_VERSION_MINOR}_${JAVA_VERSION_UPDATE}.tar.gz | tar xz -C /usr/lib/jvm/ && \
+    # remove apt cache from image
+    apt-get clean all
 
-    # set compatible home path
-    ln -s /usr/lib/jvm/jdk-${JAVA_VERSION_MAJOR}.${JAVA_VERSION_MINOR}+${JAVA_VERSION_UPDATE} /usr/lib/jvm/default-jvm && \
-    ln -s /usr/lib/jvm/default-jvm/bin/java /usr/bin/java && \
+RUN mkdir -p /usr/lib/jvm/ && \
 
+    # Install java
+    /usr/local/bin/set_java_ver ${JAVA_VERSION_MAJOR} && \
 
-    # remove apt cache from image
-    apt-get clean all
+    ln -s /usr/lib/jvm/default-jvm/bin/java /usr/bin/java
 
 # expose minecraft port
 EXPOSE 25565
diff --git a/README.md b/README.md
index 406ea3b..2051709 100644
--- a/README.md
+++ b/README.md
@@ -328,6 +328,21 @@ and you should get an output simular to
 
 and the versionlist is updated.
 
+## Building the Docker Image
+To build this image for your machine you can simply use the normal docker build command
+
+	docker build -t spigot ./
+
+If you want to build this image for multiple architectures you need to use a slightly different set of commands. However this allows you to push a single image from which clients can automatically select the correct architecture.
+
+First set up a binfmt container which allows other architectures to be emulated
+
+	docker run --privileged --rm tonistiigi/binfmt --install all
+
+Then build the images and push (docker doesn't currently support loading multi-arch images directly)
+
+	docker buildx build --platform linux/amd64,linux/arm/v7,linux/arm64 --tag scratchcat1/minecraft-spigot:latest --push ./
+
 ## Old versions news
 
 - Updated java version to 17 which is a LTS version
diff --git a/rootfs/usr/local/bin/set_java_ver b/rootfs/usr/local/bin/set_java_ver
index 10ef1b9..4121f2e 100755
--- a/rootfs/usr/local/bin/set_java_ver
+++ b/rootfs/usr/local/bin/set_java_ver
@@ -6,10 +6,30 @@
 # (c) 2021 nimmis <kjell.havneskold@gmail.com>
 #
 ###########################################################
+
+function get_cpu_arch_for_java () {
+  cpu_arch_line=$(lscpu | grep Architecture)
+  case "$cpu_arch_line" in
+    *x86_64*)
+      echo "x64"
+      ;;
+    *armv7*)
+      echo "arm"
+      ;;
+    *armv8*) # unconfirmed
+      echo "aarch64"
+      ;;
+    *)
+      echo "Unknown processor architecture. Please file a bug report."
+      ;;
+  esac
+}
+
 function set_java_17 () {
   if ! [ -d /usr/lib/jvm/jdk-17* ]; then
-    echo "Downloading JDK 1.17"
-    curl -L https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.1%2B12/OpenJDK17U-jdk_x64_linux_hotspot_17.0.1_12.tar.gz  | tar xz -C /usr/lib/jvm
+    CPU_ARCH=$(get_cpu_arch_for_java)
+    echo "Downloading JDK 1.17 $CPU_ARCH"
+    curl -L "https://github.com/adoptium/temurin17-binaries/releases/download/jdk-17.0.2%2B8/OpenJDK17U-jdk_${CPU_ARCH}_linux_hotspot_17.0.2_8.tar.gz"  | tar xz -C /usr/lib/jvm
   fi
 
   echo "set java version to 17"
@@ -21,8 +41,9 @@ function set_java_17 () {
 
 function set_java_16 () {
   if ! [ -d /usr/lib/jvm/jdk-16* ]; then
-    echo "Downloading JDK 1.16"
-    curl -L https://github.com/adoptium/temurin16-binaries/releases/download/jdk-16.0.2%2B7/OpenJDK16U-jdk_x64_linux_hotspot_16.0.2_7.tar.gz  | tar xz -C /usr/lib/jvm
+    CPU_ARCH=$(get_cpu_arch_for_java)
+    echo "Downloading JDK 1.16 $CPU_ARCH"
+    curl -L "https://github.com/adoptium/temurin16-binaries/releases/download/jdk-16.0.2%2B7/OpenJDK16U-jdk_${CPU_ARCH}_linux_hotspot_16.0.2_7.tar.gz"  | tar xz -C /usr/lib/jvm
   fi
 
   echo "set java version to 16"
@@ -33,11 +54,12 @@ function set_java_16 () {
 
 function set_java_11 () {
   if ! [ -d /usr/lib/jvm/jdk-11* ]; then
-    echo "Downloading JDK 1.11"
-    curl -L https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.13%2B8/OpenJDK11U-jdk_x64_linux_hotspot_11.0.13_8.tar.gz  | tar xz -C /usr/lib/jvm
+    CPU_ARCH=$(get_cpu_arch_for_java)
+    echo "Downloading JDK 1.11 $CPU_ARCH"
+    curl -L "https://github.com/adoptium/temurin11-binaries/releases/download/jdk-11.0.14.1%2B1/OpenJDK11U-jdk_${CPU_ARCH}_linux_hotspot_11.0.14.1_1.tar.gz"  | tar xz -C /usr/lib/jvm
   fi
 
-  echo "set java version to 16"
+  echo "set java version to 11"
   rm -f /usr/lib/jvm/default-jvm 
   ln -s /usr/lib/jvm/jdk-11* /usr/lib/jvm/default-jvm
 
@@ -45,8 +67,9 @@ function set_java_11 () {
 
 function set_java_8 () {
   if ! [ -d /usr/lib/jvm/jdk8* ]; then
-    echo "Downloading JDK 1.8"
-    curl -L https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u312-b07/OpenJDK8U-jdk_x64_linux_hotspot_8u312b07.tar.gz  | tar xz -C /usr/lib/jvm
+    CPU_ARCH=$(get_cpu_arch_for_java)
+    echo "Downloading JDK 1.8 $CPU_ARCH"
+    curl -L "https://github.com/adoptium/temurin8-binaries/releases/download/jdk8u322-b06/OpenJDK8U-jdk_${CPU_ARCH}_linux_hotspot_8u322b06.tar.gz"  | tar xz -C /usr/lib/jvm
   fi
 
   echo "set java version to 8"
@@ -74,7 +97,7 @@ case "$1" in
     echo "set_java_ver <version>"
     echo "set the java version for the container"
     echo 
-    echo "Available versions are 8, 11, 16 and 17"
+    echo "Available versions is 8, 11, 16 and 17"
     exit 1
   ;;
 esac

From d96e1eda60c6788b4c7ea4b47f805288411594a7 Mon Sep 17 00:00:00 2001
From: Connor Holloway <c.holloway314@outlook.com>
Date: Mon, 14 Mar 2022 21:50:57 +0000
Subject: [PATCH 2/3] Fix text error

---
 rootfs/usr/local/bin/set_java_ver | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/rootfs/usr/local/bin/set_java_ver b/rootfs/usr/local/bin/set_java_ver
index 4121f2e..375a3d1 100755
--- a/rootfs/usr/local/bin/set_java_ver
+++ b/rootfs/usr/local/bin/set_java_ver
@@ -97,7 +97,7 @@ case "$1" in
     echo "set_java_ver <version>"
     echo "set the java version for the container"
     echo 
-    echo "Available versions is 8, 11, 16 and 17"
+    echo "Available versions are 8, 11, 16 and 17"
     exit 1
   ;;
 esac

From 424621ca970e3e15741502461efd262a554d2ee1 Mon Sep 17 00:00:00 2001
From: Connor Holloway <c.holloway314@outlook.com>
Date: Wed, 16 Mar 2022 17:05:12 +0000
Subject: [PATCH 3/3] Add libatomic1 and avoid installing Java early

- libatomic1 is a depencency for Java on armv7 but not amd64.
- Installing Java early while performing a multi-arch build results in Java
for the host CPU being installed rather than the correct architecture
---
 Dockerfile | 5 +----
 1 file changed, 1 insertion(+), 4 deletions(-)

diff --git a/Dockerfile b/Dockerfile
index 1e73cc0..64e5bb5 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -39,7 +39,7 @@ RUN apt-get update && \
     printf "Build of nimmis/spigot:latest, date: %s\n"  `date -u +"%Y-%m-%dT%H:%M:%SZ"` > /etc/BUILDS/spigot && \
 
     # install application
-    apt-get install -y wget git jq && \
+    apt-get install -y wget git jq libatomic1 && \
 
     # Make special user for minecraft to run in
     /usr/sbin/useradd -s /bin/bash -d /minecraft -m minecraft && \
@@ -62,9 +62,6 @@ RUN apt-get update && \
 
 RUN mkdir -p /usr/lib/jvm/ && \
 
-    # Install java
-    /usr/local/bin/set_java_ver ${JAVA_VERSION_MAJOR} && \
-
     ln -s /usr/lib/jvm/default-jvm/bin/java /usr/bin/java
 
 # expose minecraft port