Skip to content

Commit

Permalink
build: multi stage docker build
Browse files Browse the repository at this point in the history
  • Loading branch information
torkelrogstad committed Jul 17, 2023
1 parent 995bfed commit 209c417
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
41 changes: 29 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
# After spending lots of time in Docker hell I don't have any more apetite for
# this. I'm lost in a sea of different JRE/JDK distributions with strange names,
# and they all give me different linker errors (at runtime!) it's completely
# impossible to debug. God, I hate the JVM.
# If someone who actually knows what they're doing comes around, it'd be great
# to separate this out to a builder/runner staged build. This leads to a
# horrendously large image.
# Example of custom Java runtime using jlink in a multi-stage container build
FROM eclipse-temurin:11 as jre-build

# Create a custom Java runtime
RUN $JAVA_HOME/bin/jlink \
--add-modules java.base \
--add-modules java.logging \
--strip-debug \
--no-man-pages \
--no-header-files \
--compress=2 \
--output /javaruntime

# Stage 1: Build stage
FROM gradle:7-jdk11 AS build

WORKDIR /workdir

COPY . .
RUN gradle build
RUN gradle fatJar

# Stage 2: Runtime stage
# Needs to be a recent ish debian image to have a new enough
# libc. Still haven't understood the finer points here.
FROM debian:bookworm-slim

ENV JAVA_HOME=/opt/java/openjdk
ENV PATH "${JAVA_HOME}/bin:${PATH}"
COPY --from=jre-build /javaruntime $JAVA_HOME

WORKDIR /app

# TODO: parameterize the version?
RUN unzip build/distributions/deskriptor-1.0-SNAPSHOT.zip && \
cp -r /workdir/deskriptor-1.0-SNAPSHOT /deskriptor
COPY --from=build /workdir/build/libs/deskriptor-1.0-SNAPSHOT-standalone.jar /app/
COPY logging.properties /logging.properties

ENTRYPOINT [ "/deskriptor/bin/deskriptor"]
ENTRYPOINT [ "java", "-jar", "/app/deskriptor-1.0-SNAPSHOT-standalone.jar" ]
19 changes: 18 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,21 @@ sourceSets["main"].java {

application {
mainClass.set("no.bb.deskriptor.DeskriptorKt")
}
}

tasks {
val fatJar = register<Jar>("fatJar") {
dependsOn.addAll(listOf("compileJava", "compileKotlin", "processResources")) // We need this for Gradle optimization to work
archiveClassifier.set("standalone") // Naming the jar
duplicatesStrategy = DuplicatesStrategy.EXCLUDE
manifest { attributes(mapOf("Main-Class" to application.mainClass)) } // Provided we set it up in the application plugin configuration
val sourcesMain = sourceSets.main.get()
val contents = configurations.runtimeClasspath.get()
.map { if (it.isDirectory) it else zipTree(it) } +
sourcesMain.output
from(contents)
}
build {
dependsOn(fatJar) // Trigger fat jar creation during build
}
}

0 comments on commit 209c417

Please sign in to comment.