Common Approach
Layered Jars
- For this to work, layers configuration needs to be enabled for
spring-boot-maven-plugin
.
2. Build the fat jar by running the following command. This will generate a fat jar in the target
directory.
$ mvn package
3. Check layers by running the following command. Note that this is my jar file name. Replace spring-boot-docker-test-0.0.1-SNAPSHOT.jar
to the jar name inside your target
directory.
$ java -Djarmode=layertools -jar ./target/spring-boot-docker-test-0.0.1-SNAPSHOT.jar listdependencies
spring-boot-loader
snapshot-dependencies
application
4. Copying each layer from the previous step is specified in the Dockerfile
. IMPORTANT : The order of COPY
instruction needs to match the layers order.
Buildpacks
Docker images can be built by running this command.
$ mvn spring-boot:build-image
If you are using gradle, use this command
$ gradle bootBuildImage
If you want to build image every time you build the project, add the build-image
goal under execution for spring-boot-maven-plugin
.
From this point on, for every mvn package
, docker image will be built automatically.
Conclusion
This article covered three different ways of creating docker images out of a Spring Boot application.
Spring Team suggests the layered jar option is the fastest in terms of creating the image and the application start up time.
The buildpack option seems very convenient because it makes it possible to create docker images without a Dockerfile. However, it means the developers will not have control over the Dockerfile (image instructions) and there will be a dependency on Paketo Buildpacks
.
Therefore, I would start with the common approach. If the process takes too long as the application gets bigger and bigger, then I would consider optimization using the layered jar approach.