This repository is intended to serve as a starting point for learning how to containerize Java applications.
It is not complete or perfect - just a playground to reduce boilerplate. Contributions are always welcome :)
- JDK 17 or higher
- Docker (or if you are more advanced an alternative solution like
podman. Thejibexample might not work this way though) - Maven
- Clone the repo and import the top-level
pom.xmlin your favourite IDE or editor. - Make sure you can actually build the project by invoking
mvn clean install
| Module | Description |
|---|---|
| 01-simple-app | A simple webserver with no dependencies just to get you started. Can be spiced up by using jlink. |
| 02-jakarta-microprofile | Shows how to package a war file with an application server as a container |
| 03-jib-spring-boot | Package a Spring Boot app without Dockerfiles but with jib instead |
There are two main tools at our disposal
jlink gives us the option to create a custom Java runtime using only the modules our application acutally uses. A common missconception is that your app needs to be fully modularized for that (i.e. it runs on the module path). That's acutally not necessary.
The 01-simple-app project gives you a starting point to explore this option. Play around with it - e.g. try adding some different JDK classes and see what happens.
The main goal of Application class-data sharing (short AppCDS) is to reduce startup time by pre-creating an archive of the classes that have to be loaded and verified by the JVM. This archive can be passed at startup and thus reduce the amount of work the JVM has to perform.
Gunnar Morling has published an interesting Article about using AppCDS with Maven
For a deeper understanding check out this In-depth article about AppCDS by Nicolai Parlog
You can of course combine the power of jlink and AppCDS. This Article will give you some guidance and also show some benchmarks at the end.
Another interesting topic is how modern Frameworks can help with the creation of container images.
Spring Boot uses Cloud native buildpacks to create OCI compliant containers. All managed through the spring-boot-maven-plugin. No Dockerfiles required!
This article (DE) gives a great overview, very much recommended
Get started at start.spring.io
New quarkus projects come with a couple of predefined Dockerfiles, one of them builds a custom Runtime with jlink. Quarkus also has out-of-the-box support for AppCDS
Get started at code.quarkus.io
Helidon provides - similar to quarkus - predefined Dockerfiles (also with a jlink option) as part of their starter.
Get started at helidon.io