Skip to content

deepaksorthiya/spring-boot-docker

Repository files navigation

Spring Boot Docker Starter Project

JVM Maven Build GraalVM Maven Build Docker Spring Boot

Getting Started

Requirements:

Git: 2.49.0
Spring Boot: 3.5.7
Maven: 3.9+
Java: 25
Docker Desktop: Tested on 4.45.0

Clone this repository:

git clone https://github.com/deepaksorthiya/spring-boot-docker.git
cd spring-boot-docker

Running Application In Different Mode and Startup Performance

1. Exploded Jar

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)

2. CDS Mode With Exploded Jar

./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)

3. Spring-AOT + CDS

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)

4. JDK 24+ AOT-Cache(Successor of CDS) + Spring-AOT

./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)

5. Project Leyden

TBD

YouTube Video

6. GraalVM Native Mode

./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>

Image Optimization

<configuration>
    <buildArgs>
        <buildArg>--pgo</buildArg>
        <buildArg>--gc=G1</buildArg>
        <buildArg>-march=native</buildArg>
    </buildArgs>
</configuration>

Buildpacks

<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>

Build Docker Image(docker should be running):

./mvnw clean spring-boot:build-image -DskipTests
  • OR for low image size and multi-stage build (Run from workspace where Dockerfile is located)
docker build --progress=plain --no-cache -f <dockerfile> -t deepaksorthiya/spring-boot-docker .
  • OR Build Using Local Fat Jar In Path target/spring-boot-docker-0.0.1-SNAPSHOT.jar
docker 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 .
  • OR if above not work try below command

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 .
  • Run In Docker Container
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

Rest APIs

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

Reference Documentation

For further reference, please consider the following sections:

About

Publish Docker Image to DockerHub and rest client examples

Topics

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors 2

  •  
  •