1 min to read
Different ways to dockerize spring-app
Method 1: Spring Boot Build Plugin
I was actually surprised that Spring Boot and Maven already have docker builder basically build-in into Maven. Plugin uses BuildPack builder which automatically detects SpringBoot app, and then it builds the relevant image for you.
mvn spring-boot:build-image -Dspring-boot.build-image.imageName=hello-jvm:buildPacks
Images are a bit large and not optimized (but that also depends on JDK that you use). In the end all dependencies and compiled application is stored in a single large layer.
Method 2: Jib
Jib organizes your app into layers; dependencies, resources, classes. It utilizes docker-image layer cache to keep builds faster (only rebuilding changes).
What are those layers?
Layer 1: adds libs
Layer 2: adds resources
Layer 3: adds classes
Layer 4: jib-classpath-file
Layer4 contains the jib-classpath-file with classpath information and the jib-main-class-file containing the name of the main class that will be executed.
How to build with jib?
Build locally > mvn compile jib:dockerBuild -Dimage=hello-jvm:jib Build and push to the registry > mvn compile jib:build -Dimage=hello-jvm:jib
Why do I prefer Jib?
In the nutshell Jib provides you with extensive level of configuration and rich tools just out of the box. Optimized layers inside the docker, registry push, ability to set JVM parameters, ports, properties etc. and it is simple to start with. Just look at the docs.
Spotify maven plugin
<plugin> <groupId>com.spotify</groupId> <artifactId>dockerfile-maven-plugin</artifactId> <!-- configuration --> </plugin>
Fabric8 maven plugin
<plugin> <groupId>io.fabric8</groupId> <artifactId>docker-maven-plugin</artifactId> <!-- configuration --> </plugin>