TExcelTAD
Home › Spring Boot › Packaging & Docker

Packaging & Docker

A Spring Boot application ships as a single executable JAR that contains your code, all dependencies and the embedded Tomcat. Run it anywhere with java -jar. For containers, Boot can build an optimised Docker image without a Dockerfile, or you can write a small multi-stage Dockerfile yourself.

Building the executable JAR

bash · try it yourself
./mvnw -q clean package -DskipTests
ls -lh target/*.jar
-rw-r--r-- 1 me me  48M Oct 16 11:40 target/tasks-0.0.1-SNAPSHOT.jar
-rw-r--r-- 1 me me  21K Oct 16 11:40 target/tasks-0.0.1-SNAPSHOT.jar.original

The .original file is the plain JAR Maven built; the Spring Boot plugin repackaged it into a "fat" JAR. Peek inside:

text
$ jar tf target/tasks-0.0.1-SNAPSHOT.jar | grep -E "^(BOOT-INF/(classes|lib)/[^/]*$|META-INF/MANIFEST)" | head -6
META-INF/MANIFEST.MF
BOOT-INF/classes/application.properties
BOOT-INF/lib/spring-boot-3.5.6.jar
BOOT-INF/lib/spring-core-6.2.11.jar
BOOT-INF/lib/jackson-databind-2.19.2.jar
BOOT-INF/lib/tomcat-embed-core-10.1.46.jar
Path Contents
BOOT-INF/classes your compiled classes and resources
BOOT-INF/lib every dependency JAR, unchanged
org/springframework/boot/loader the launcher that loads nested JARs
META-INF/MANIFEST.MF Main-Class: …JarLauncher, Start-Class: com.example.tasks.TasksApplication
bash · try it yourself
java -jar target/tasks-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod --server.port=8080
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/

 :: Spring Boot ::                (v3.5.6)

2025-10-16T11:41:03.002+07:00  INFO 54001 --- [tasks] [           main] com.example.tasks.TasksApplication       : The following 1 profile is active: "prod"
2025-10-16T11:41:05.118+07:00  INFO 54001 --- [tasks] [           main] com.example.tasks.TasksApplication       : Started TasksApplication in 2.731 seconds (process running for 3.090)

Option 1: Cloud Native Buildpacks (no Dockerfile)

bash · try it yourself
./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=example/tasks:1.4.0 | tail -3
docker images example/tasks
[INFO] Successfully built image 'docker.io/example/tasks:1.4.0'
[INFO]
[INFO] BUILD SUCCESS
REPOSITORY      TAG       IMAGE ID       CREATED        SIZE
example/tasks   1.4.0     5c1d9e2f7a30   45 years ago   312MB

The buildpack picks a JRE, sets memory flags based on the container limit, runs as a non-root user and splits the app into layers. ("45 years ago" is not a bug: buildpacks use a fixed timestamp so builds are reproducible.)

Option 2: a multi-stage Dockerfile

dockerfile
# ---- build stage ----
FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY mvnw pom.xml ./
COPY .mvn .mvn
RUN ./mvnw -q dependency:go-offline
COPY src src
RUN ./mvnw -q package -DskipTests \
 && java -Djarmode=tools -jar target/*.jar extract --layers --launcher --destination /extracted

# ---- runtime stage ----
FROM eclipse-temurin:21-jre
RUN useradd --system --uid 1001 app
USER app
WORKDIR /app
COPY --from=build /extracted/dependencies/ ./
COPY --from=build /extracted/spring-boot-loader/ ./
COPY --from=build /extracted/snapshot-dependencies/ ./
COPY --from=build /extracted/application/ ./
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "org.springframework.boot.loader.launch.JarLauncher"]

Key ideas:

Technique Why
multi-stage build the final image contains a JRE, not the JDK, Maven or sources
copy pom.xml first, then src dependency downloads are cached until the POM changes
layered extraction dependencies (rarely change) and your classes (change often) become separate image layers
non-root user limits damage if the app is compromised
MaxRAMPercentage sizes the heap relative to the container memory limit

Why layers matter

Docker reuses a layer when its content is unchanged. When only your code changes, only the small application layer is rebuilt and pushed. This program compares the upload size of a single "fat JAR" layer against a layered image for a typical code-only change:

java · try it yourself
import java.util.LinkedHashMap;
import java.util.Map;

class Main {
    public static void main(String[] args) {
        Map<String, Integer> layersMb = new LinkedHashMap<>();
        layersMb.put("dependencies", 46);
        layersMb.put("spring-boot-loader", 1);
        layersMb.put("snapshot-dependencies", 0);
        layersMb.put("application", 1);

        int total = layersMb.values().stream().mapToInt(Integer::intValue).sum();
        System.out.println("Fat JAR in one layer: every release pushes " + total + " MB");

        int pushed = 0;
        for (var e : layersMb.entrySet()) {
            boolean changed = e.getKey().equals("application");     // only our code changed
            if (changed) pushed += e.getValue();
            System.out.printf("  %-22s %3d MB  %s%n", e.getKey(), e.getValue(), changed ? "pushed" : "cached");
        }
        System.out.println("Layered image: this release pushes " + pushed + " MB");
        System.out.printf("Saving per release: %.0f%%%n", 100.0 * (total - pushed) / total);
    }
}
Fat JAR in one layer: every release pushes 48 MB
  dependencies            46 MB  cached
  spring-boot-loader       1 MB  cached
  snapshot-dependencies    0 MB  cached
  application              1 MB  pushed
Layered image: this release pushes 1 MB
Saving per release: 98%

Running the container

bash · try it yourself
docker run -d --name tasks -p 8080:8080 \
  -e SPRING_PROFILES_ACTIVE=prod \
  -e SPRING_DATASOURCE_URL=jdbc:postgresql://db:5432/tasks \
  -e SPRING_DATASOURCE_USERNAME=tasks -e SPRING_DATASOURCE_PASSWORD=change-me \
  example/tasks:1.4.0
sleep 5 && curl -s localhost:8080/actuator/health
8f3b1d2c9e4a7f6b5c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7c8d9e0f1a2b
{"status":"UP"}

Configuration arrives through environment variables, so the same image runs in every environment.

Tip: Enable graceful shutdown so running requests can finish when the container is stopped: server.shutdown=graceful (the default since Boot 3.4) and spring.lifecycle.timeout-per-shutdown-phase=20s.

Test yourself

Where are your application's dependencies stored inside a Spring Boot fat JAR?

  1. lib/
  2. BOOT-INF/lib/
  3. META-INF/deps/
  4. They are downloaded at startup

What is the main benefit of a layered Docker image for a Spring Boot app?

  1. Smaller JVM
  2. Unchanged dependency layers are cached, so releases push only the small application layer
  3. It removes the need for a JRE
  4. Faster SQL queries

Which command builds an OCI image with buildpacks, without a Dockerfile?

  1. ./mvnw docker:build
  2. ./mvnw spring-boot:build-image
  3. docker build .
  4. java -jar --image