From 1e5033b46195128cce745bf6486ea6487f4962c6 Mon Sep 17 00:00:00 2001 From: Matt Cengia Date: Fri, 17 Jul 2020 13:33:50 +1000 Subject: [PATCH] Don't require Gradle to build Docker image Update Dockerfile to use a multi-stage build and run Gradle *within* the builder container, rather than on the host, so the host needn't have Gradle or JDK installed, then copy the build .jar from the builder container into the final container. Update build.gradle's dockerBuild target to not build the jar, but instead do it within the Docker build process. This results in some double-handling because it requires Gradle and JDK both on the host system *and* within the container, and runs two instances of Gradle during the build, but is added for backwards compatibility. The better approach (rather than running `./gradlew dockerBuild`) is to manually run `docker build -t ma1ua/ma1sd .`. I've tested this process on both a Debian amd64 system and a Raspberry Pi 3 running Raspbian, and it seems to work (though the RPi is pretty slow). --- Dockerfile | 10 +++++++++- build.gradle | 2 +- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 8a38986..5289b04 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,3 +1,11 @@ +FROM openjdk:8-jre-alpine AS builder + +RUN apk update && apk add gradle git && rm -rf /var/lib/apk/* /var/cache/apk/* + +WORKDIR /ma1sd +COPY . . +RUN ./gradlew shadowJar + FROM openjdk:8-jre-alpine RUN apk update && apk add bash && rm -rf /var/lib/apk/* /var/cache/apk/* @@ -15,4 +23,4 @@ CMD [ "/start.sh" ] ADD src/docker/start.sh /start.sh ADD src/script/ma1sd /app/ma1sd -ADD build/libs/ma1sd.jar /app/ma1sd.jar +COPY --from=builder /ma1sd/build/libs/ma1sd.jar /app/ma1sd.jar diff --git a/build.gradle b/build.gradle index fa081b2..bed09a6 100644 --- a/build.gradle +++ b/build.gradle @@ -264,7 +264,7 @@ task debBuild(dependsOn: shadowJar) { } } -task dockerBuild(type: Exec, dependsOn: shadowJar) { +task dockerBuild(type: Exec) { commandLine 'docker', 'build', '-t', dockerImageTag, project.rootDir doLast {