The recommended deployment practice for Java these days (embedded custom runtime, created with jlink) never requires any pre-installed runtime. The end-user should never install a runtime separately from the application.
We do. As of this pr, you can use distroless with your own jlink + spring boot.
FROM openjdk:jdk-buster as jreBuilder
RUN jlink \
--add-modules jdk.unsupported,java.sql,java.desktop,java.naming,java.management,java.instrument,java.security.jgss,java.rmi \
--verbose \
--strip-debug \
--compress 2 \
--no-header-files \
--no-man-pages \
--output /jre
FROM gcr.io/distroless/java-debian10:base
ARG JAR_FILE
COPY --from=jreBuilder /jre /usr/lib/jre
ENTRYPOINT ["/usr/lib/jre/bin/java", "-jar", "./app.jar"]
COPY ./target/${JAR_FILE} ./app.jar
If you're using modules already, you won't need to specify all those modules manually. Also the base image could be extracted out, or you could let docker cache the layer for you.
java:base is necessary because there are a few libraries that aren't repackaged by jlink like libz or image libs. Although that would be neat, jlink being native library aware. The tool could know if you have a dependency on java.desktop, which would then have a dependency on libpng and libjpeg. /u/pron98 do you know if there is any tooling around associating native libraries with the java modules that require them?
afaik libz is required at startup by the jvm, other ones like libjpeg are only necessary when the imageio libraries are called.
here's the full list of libraries required by java, on top of the onces from base.
How is the "recommended" deployment practice something most don't use?
Because it's relatively new.
You'll probably still use java -jar with jlink images, too (unless your application is modularised), it's just that the runtime is very small and is part of the app.
Is there a Spring Boot starter or guide to use jlink?
The only thing you'll need to do different is list more JDK modules at the end. You figure out which modules you use with jdeps (this step is unnecessary if your application is modularised, but most aren't).
ou figure out which modules you use with jdeps (this step is unnecessary if your application is modularised, but most aren't).
If you are using Gradle, here is a small task that prints the modules required for your jar (or uberjar)
val printModuleDeps by tasks.registering {
description = "Print Java Platform Module dependencies of the application."
group = LifecycleBasePlugin.BUILD_TASK_NAME
doLast {
val uberJar = named("shadowJar", Jar::class)
val jarFile = uberJar.get().archiveFile.get().asFile
val jdeps = ToolProvider.findFirst("jdeps").orElseGet { error("jdeps not found") }
val out = StringWriter()
val pw = PrintWriter(out)
jdeps.run(pw, pw, "--print-module-deps", "--ignore-missing-deps", jarFile.absolutePath)
val modules = out.toString()
println(modules)
}
dependsOn("shadowJar")
}
Basically, they go through your dependencies, and pull out platform modules they need, either from the module declaration or just calling jdeps
Jlink makes your build platform specific though. I’ve made custom build plugins to cache a Linux JDK so people can build on whatever and cross target the jlink build - you basically just point to the jmods directory in the jlink call. We only run Linux containers. So we use jlink just for that platform.
The result is a much smaller distribution, and the Java dev team now uses a Java version controlled by our CI environment, which makes our ops team much happier. They don’t typically work in Java and like not dealing with its dependencies
Additionally, I’ve added jib core to use the jlink output to just overlay on top of our “official” minimal base images, similar to distroless, which was quite easy. This means our container builds are all done via Maven, without any need for a docker daemon.
The workflow is still new, so I expect open source projects to take some time to iron out the kinks.
Or, I expect that jlink packing to take some time with common build tools.
Current build tools, like Maven, haven’t had platform specific output as a convention. Doing things like cross targeting is possible it’s just not a part of the ecosystem.
You could, for example, use the Maven “classifier” to indicate platform but then you couldn’t use it for anything else.
So, I expect custom runtimes to be a slow adoption. As everyone figures out how to account for this.
In the meantime, most server projects only ship on one very controlled platform, and honestly a custom plugin is not that hard to build.
11
u/mxxxz Mar 11 '21
Does that mean you can run a Spring Boot application without having JVM installed in the server or in the dockerfiles?