Scrivio Interview library
ConceptFoundations6 min read · updated 12 November 2026

Compare building a container image for a Spring Boot app with a simple Dockerfile copying the fat jar, with layered jars, and with Cloud Native Buildpacks via spring-boot:build-image.

A simple Dockerfile that copies the fat jar puts all dependencies and our code in one layer, so every code change produces and uploads a new layer of 50 to 100 MB. Layered jars split the content into dependencies, the Spring Boot loader, snapshot dependencies and application code, so a code change only rebuilds a small top layer. Cloud Native Buildpacks, through spring-boot:build-image, build a layered image with a JRE, a memory calculator and a non-root user without any Dockerfile, giving less control but a consistent result.

Why this question is asked

Every Spring Boot service ends up in a container image, and the way we build it decides how fast builds and deployments are, how much registry storage we use, and how secure the result is. Interviewers ask this to see whether we understand image layers and the tools Spring Boot provides around them. In this answer, we will recap how image layers are cached, build the fat jar image and see its weakness, improve it with layered jars, compare Buildpacks, and finish with the basic hardening every image should have.

1A Quick Recap: Image Layers and Caching

A container image is a stack of layers. Each instruction in a Dockerfile that adds files, such as COPY or RUN, creates a new layer, identified by a hash of its content. When we push an image, the registry only receives layers it does not already have, and when a node pulls an image, it only downloads layers it does not have locally. The build cache works the same way: if a layer's input has not changed, the builder reuses it.

So the goal is simple: put things that rarely change, such as the JRE and third-party libraries, in lower layers, and things that change on every commit, our own classes, in the top layer. Then a typical release only moves a few hundred kilobytes.

2Approach 1: Copy the Fat Jar

Spring Boot packages an application as an executable fat jar: our classes plus every dependency jar inside one file, with a small loader that knows how to run it. The simplest Dockerfile copies that file.

Java
FROM eclipse-temurin:25-jre
COPY target/orders-1.0.jar /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
# Change one line of code, rebuild and push:
# => layer for /app/app.jar: 78 MB, new hash, uploaded again

This works and is easy to understand. But the jar is one file, so any change to it changes the whole layer. A one-line fix means uploading and pulling 78 MB, of which 77.8 MB are libraries that did not change. With 30 services deploying several times a day, that is a lot of registry traffic and slower rollouts on new nodes.

3Approach 2: Layered Jars

Spring Boot jars include a layers index that groups their content into four layers by default: dependencies (released third-party jars), spring-boot-loader, snapshot-dependencies, and application (our classes and resources). Since Spring Boot 3.3, the jar's tools mode extracts these into separate directories; older versions used -Djarmode=layertools with an extract command. We copy each directory with its own COPY instruction, so each becomes its own image layer.

Text
FROM eclipse-temurin:25-jre AS builder
WORKDIR /builder
COPY target/orders-1.0.jar app.jar
RUN java -Djarmode=tools -jar app.jar extract --layers --launcher --destination extracted
FROM eclipse-temurin:25-jre
RUN useradd --system --uid 10001 app
USER 10001
WORKDIR /app
COPY --from=builder /builder/extracted/dependencies/ ./ # about 75 MB, rarely changes
COPY --from=builder /builder/extracted/spring-boot-loader/ ./ # tiny, changes with Boot
COPY --from=builder /builder/extracted/snapshot-dependencies/ ./ # changes with snapshots
COPY --from=builder /builder/extracted/application/ ./ # about 300 KB, every commit
ENTRYPOINT ["java", "org.springframework.boot.loader.launch.JarLauncher"]
Step throughLayered Spring Boot image, top changes most
  1. applicationour classes and resources: changes every commit
  2. snapshot-dependenciesSNAPSHOT libraries: changes often
  3. spring-boot-loaderlauncher classes: changes with Boot upgrades
  4. dependenciesreleased libraries: changes rarely
  5. JRE base imageOS and Java runtime: changes with patches

5 layers, from the top down. Step through them one at a time.

A code change rebuilds and moves only the top layer.

Now a code change only alters the last layer. The push sends a few hundred kilobytes, and nodes that already ran the previous version only pull that layer. Notice that the --launcher option keeps the Spring Boot loader, so we start the app with JarLauncher; without --launcher, extract produces a plain jar with a lib directory that we run with java -jar, which is also the form that works with class data sharing.

4Approach 3: Cloud Native Buildpacks

Cloud Native Buildpacks turn source or a jar into an image without a Dockerfile. Spring Boot integrates them: ./mvnw spring-boot:build-image or ./gradlew bootBuildImage runs the Paketo buildpacks in a builder container. They detect a Spring Boot app, pick a JRE, use the jar's layers, add a memory calculator that sets -Xmx and other memory flags from the container limit at startup, run the app as a non-root user, and add labels with a bill of materials.

Shell
$ ./mvnw spring-boot:build-image -Dspring-boot.build-image.imageName=registry.example.com/orders:1.0
[INFO] > Running creator
[INFO] [creator] Paketo Buildpack for BellSoft Liberica ... JRE 25
[INFO] [creator] Paketo Buildpack for Spring Boot ... Creating slices from layers index
[INFO] Successfully built image 'registry.example.com/orders:1.0'

The trade-off is control. We configure Buildpacks through environment variables and plugin settings rather than writing each step, and the builder decides the base image and its layout. In return, every service gets the same structure, and updating the base for all of them is a matter of rebuilding with a newer builder. Exact buildpack and JRE names depend on the builder version, so check your version.

5Choosing Between Them, and Basic Hardening

For a small number of services with special needs, a layered Dockerfile gives full control. For many services, Buildpacks give consistency at scale. The plain fat-jar Dockerfile is fine for a demo but wastes bandwidth in real pipelines.

Whatever we choose, three rules apply. Use a JRE base image, not a full JDK, because we do not need the compiler and tools at runtime and they add size and vulnerabilities. Run as a non-root user, so a compromised process cannot modify the image's files or use root privileges. And pin versions, the base image tag or better its digest, and the buildpack builder, so builds are reproducible and upgrades are deliberate.

Three ways to build the image
ApproachRebuild after code changeControlEffort
Fat jar Dockerfilewhole jar layer, 50 to 100 MBfulllowest
Layered jar Dockerfileapplication layer, KBsfullmedium
Buildpacks build-imageapplication layer, KBslimited, by configlowest at scale
Layered jars and Buildpacks share the caching benefit.

6Follow-up questions

Does the layered approach make startup faster?

Slightly, because the JVM reads classes from an exploded directory rather than nested jars, but the main benefit is caching. For real startup gains we combine the extracted layout with class data sharing or the JDK's AOT cache.

Can I define my own layers?

Yes. The Spring Boot Maven and Gradle plugins accept a layers configuration that lets us add a layer, for example for company-internal libraries that change more often than third-party ones but less often than our code.

Do Buildpacks need Docker on the build machine?

The Spring Boot plugins need a Docker-compatible daemon to run the builder container, or a compatible alternative such as Podman with the right socket configured. Many CI systems provide this; others use the pack CLI or kpack in the cluster instead.

7Conclusion

In this answer, we saw how image layers make caching work and how three approaches use them. Copying the fat jar is simple but puts everything in one layer that changes with every commit. Layered jars, extracted with java -Djarmode=tools -jar app.jar extract --layers --launcher on Boot 3.3 and later, separate dependencies, the loader, snapshots and our code so only a small top layer changes. Buildpacks through spring-boot:build-image produce a consistent layered image with a JRE, a memory calculator and a non-root user, with less control. In every case we use a JRE base, a non-root user and pinned versions.

What to say in the interviewAbout 80 seconds aloud

Open with

The key idea is image layers. Registries and nodes only move layers that changed, so we want libraries low and our code on top. A simple Dockerfile that copies the fat jar puts all of it in one layer, so a one-line change means pushing and pulling the full 70 or 80 MB again.

Then, if they want more

With layered jars, Spring Boot's layer index splits the jar into dependencies, spring-boot-loader, snapshot-dependencies and application. On Boot 3.3 and later I extract them with java -Djarmode=tools -jar app.jar extract --layers --launcher, older versions used layertools, and copy each directory with its own COPY in a multi-stage Dockerfile, then start with JarLauncher.

A code change now only touches the application layer of a few hundred kilobytes. Buildpacks, through spring-boot:build-image or bootBuildImage, do all this without a Dockerfile: the Paketo buildpacks pick a JRE, use the layers, add a memory calculator that sets the heap from the container limit, and run as non-root.

You trade control for consistency, which I like for many services. Whatever the approach, I use a JRE rather than a JDK base, a non-root user, and pinned versions or digests for the base image and the builder.

About this answer

Last updated
12 November 2026
Versions it names
Spring Boot 3.3
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Official documentation
Kubernetes documentation
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "Compare building a container image for a Spring Boot app with a simple Dockerfile...": https://scrivio-interview-library.netlify.app/spring-boot/containers-and-kubernetes/a42efe6ec7/