spring boot docker container native image graalvm
3.8K
Git: 2.51+
Spring Boot: 4.0.3
Maven: 3.9+
Java: 25
Docker Desktop: Tested on 4.50+
git clone https://github.com/deepaksorthiya/spring-boot-docker.git
cd spring-boot-docker
Build Project
./mvnw clean package -DskipTests
Remove directory application if exists
rm -f -R application
Create Self Exploded Jar file
java -Djarmode=tools -jar target/spring-boot-docker-0.0.1-SNAPSHOT.jar extract --destination application
Run Self Exploded Jar file
java -jar .\application\spring-boot-docker-0.0.1-SNAPSHOT.jar
Started Application in 2.309 seconds (process running for 2.54)
./mvnw clean package -DskipTests
rm -f -R application
java -Djarmode=tools -jar target/spring-boot-docker-0.0.1-SNAPSHOT.jar extract --destination application
cd application
Training Run
java -XX:ArchiveClassesAtExit=application.jsa "-Dspring.context.exit=onRefresh" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Production Run
java -XX:SharedArchiveFile=application.jsa -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Started Application in 1.338 seconds (process running for 1.487)
Build Project With AOT mode
./mvnw -Pnative -DskipTests clean package
rm -f -R application
java -Djarmode=tools -jar target/spring-boot-docker-0.0.1-SNAPSHOT.jar extract --destination application
cd application
java -XX:ArchiveClassesAtExit=application.jsa "-Dspring.context.exit=onRefresh" "-Dspring.aot.enabled=true" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
java -XX:SharedArchiveFile=application.jsa "-Dspring.aot.enabled=true" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Started Application in 0.92 seconds (process running for 1.066)
./mvnw -Pnative -DskipTests clean package
java -Djarmode=tools -jar target/spring-boot-docker-0.0.1-SNAPSHOT.jar extract --destination application
cd application
Execute the AOT cache training run
java -XX:AOTMode=record -XX:AOTConfiguration=app.aotconf "-Dspring.aot.enabled=true" "-Dspring.context.exit=onRefresh" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Create the AOT cache
java -XX:AOTMode=create -XX:AOTConfiguration=app.aotconf -XX:AOTCache=app.aot "-Dspring.aot.enabled=true" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Remove aotconf as its unnecessary now
rm app.aotconf
Run AOT cache enabled
java -XX:AOTCache=app.aot "-Dspring.aot.enabled=true" -jar spring-boot-docker-0.0.1-SNAPSHOT.jar
Started Application in 0.817 seconds (process running for 0.988)
TBD
./mvnw clean -Pnative -DskipTests native:compile
./mvnw clean -Pnative spring-boot:build-image -DskipTests
Enable Spring AOT
and add
<BP_SPRING_AOT_ENABLED>true</BP_SPRING_AOT_ENABLED>
<BP_JVM_CDS_ENABLED>true</BP_JVM_CDS_ENABLED>
<executions>
<execution>
<id>process-aot</id>
<goals>
<goal>process-aot</goal>
</goals>
</execution>
</executions>
<configuration>
<buildArgs>
<buildArg>--pgo</buildArg>
<buildArg>--gc=G1</buildArg>
<buildArg>-march=native</buildArg>
</buildArgs>
</configuration>
<env>
<!-- For native build -->
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
<!-- For cds-aot build -->
<BP_SPRING_AOT_ENABLED>true</BP_SPRING_AOT_ENABLED>
<BP_JVM_CDS_ENABLED>true</BP_JVM_CDS_ENABLED>
</env>
<env>
<BP_NATIVE_IMAGE>true</BP_NATIVE_IMAGE>
<BP_JVM_VERSION>25</BP_JVM_VERSION>
<BP_NATIVE_IMAGE_BUILD_ARGUMENTS>-march=native</BP_NATIVE_IMAGE_BUILD_ARGUMENTS>
</env>
./mvnw clean spring-boot:build-image -DskipTests
docker build --progress=plain --no-cache -f <dockerfile> -t deepaksorthiya/spring-boot-docker .
target/spring-boot-docker-0.0.1-SNAPSHOT.jardocker build --build-arg JAR_FILE=target/spring-boot-docker-0.0.1-SNAPSHOT.jar -f Dockerfile.jvm --no-cache --progress=plain -t deepaksorthiya/spring-boot-docker .
you should be in jar file path to work build args
cd target
docker build --build-arg JAR_FILE=spring-boot-docker-0.0.1-SNAPSHOT.jar -f ./../Dockerfile.jvm --no-cache --progress=plain -t deepaksorthiya/spring-boot-docker .
docker run -p 8080:8080 --name spring-boot-docker -it deepaksorthiya/spring-boot-docker
| Dockerfile Name | Description |
|---|---|
| Dockerfile | multi stage docker file with Spring AOT and JDK24+ AOT Cache |
| Dockerfile.jlink | single stage using JDK jlink feature to reduce size |
| Dockerfile.jvm | single stage using with Spring AOT and JDK24+ AOT Cache |
| Dockerfile.native | single stage using graalvm native image using oraclelinux 9 |
| Dockerfile.native-distro | single stage using graalvm native image distroless linux image |
| Dockerfile.native-micro | single stage using graalvm native image micro linux image |
| Dockerfile.native-multi | multi stage using graalvm native image micro linux image |
| Dockerfile.springlayeredjar | multi stage using spring layererd layout jar |
| Dockerfile.springlayoutjar | multi stage using spring layout jar |
Note: In Dockerfile.jlink check /optimized-jdk-25. This will be created under OS root path
while onlyoptimized-jdk-25 (without slash) created path
specified in WORKDIR, Which is in this case /workspace/app
http://localhost:8080/
http://localhost:8080/server-info
http://localhost:8080/rest-client
http://localhost:8080/rest-client-error
http://localhost:8080/rest-server-error
http://localhost:8080/rest-client-delay
For further reference, please consider the following sections:
Content type
Image
Digest
sha256:918ce1e5c…
Size
36.2 MB
Last updated
7 months ago
docker pull deepaksorthiya/spring-boot-docker