Scrivio Interview library
CodingIntermediate7 min read · updated 12 November 2026

Write a multi-stage Dockerfile for a Boot application that uses layered jar extraction, and explain the order of COPY instructions.

The first stage copies the jar into a JRE image and runs java -Djarmode=tools -jar app.jar extract --layers to split it into four directories. The second stage starts from a pinned JRE base image, creates a non-root user, and copies the directories in the order dependencies, spring-boot-loader, snapshot-dependencies and application, from least to most often changed, so that most rebuilds reuse every layer except the last small one. The entrypoint runs java -jar on the extracted application jar.

Why this question is asked

A Dockerfile is a small file with a large effect: it decides how long every CI build takes, how much is pushed to the registry on each commit, how fast new nodes can pull the image and what the running process is allowed to do. Interviewers ask for this one because it combines Boot's layered jars with Docker's cache rules and basic container security. In this answer, we will write the Dockerfile, go through the extract stage, explain the order of the COPY instructions with the cache in mind, run as a non-root user, and pin the base image.

1The Complete Dockerfile

Here is the whole file first; we will go through it piece by piece afterwards. It assumes the jar was built by Maven or Gradle before docker build runs, which is the usual setup in CI.

Text
# syntax=docker/dockerfile:1
# ---- stage 1: split the Boot jar into layers ----
FROM eclipse-temurin:25-jre-noble AS extract
WORKDIR /build
COPY target/orders-*.jar app.jar
RUN java -Djarmode=tools -jar app.jar extract --layers --destination extracted
# ---- stage 2: the runtime image ----
FROM eclipse-temurin:25-jre-noble
RUN groupadd --system app && useradd --system --gid app --no-create-home app
WORKDIR /app
COPY --from=extract /build/extracted/dependencies/ ./
COPY --from=extract /build/extracted/spring-boot-loader/ ./
COPY --from=extract /build/extracted/snapshot-dependencies/ ./
COPY --from=extract /build/extracted/application/ ./
USER app
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]

A multi-stage build uses several FROM instructions in one file. Only the last stage becomes the image; earlier stages are scratch space whose results we copy out with COPY --from. That lets us run the extraction tool without leaving the original 95 MB jar inside the final image.

2Stage 1: Extracting the Layers

The first stage needs only a JRE, because the extraction tool is part of the Boot jar itself. java -Djarmode=tools starts the jar in a tool mode instead of starting the application, and extract --layers reads BOOT-INF/layers.idx and writes one directory per layer under extracted/: dependencies with the library jars, spring-boot-loader, snapshot-dependencies, and application with our code. Together, the directories form an application jar app.jar with a lib/ folder next to it, and that jar's manifest lists the libraries on its Class-Path, so it starts with plain java -jar.

Notice that the tools jar mode exists since Boot 3.3. For Boot 2.3 to 3.2, the equivalent line is RUN java -Djarmode=layertools -jar app.jar extract, and the entrypoint then starts the launcher class instead, org.springframework.boot.loader.launch.JarLauncher on 3.2 or org.springframework.boot.loader.JarLauncher before it.

3Why the COPY Order Matters

Docker builds the final stage instruction by instruction, and each COPY becomes a layer whose cache key is the content it copies. When a layer's content changes, Docker rebuilds that layer and every layer after it, even if their own content is unchanged. So the order of the COPY instructions decides how much a typical change costs.

What a code-only commit rebuilds
InstructionSizeCached on a code change?
FROM eclipse-temurin:25-jre-nobleabout 200 MByes
COPY dependencies/90 MByes
COPY spring-boot-loader/0.5 MByes
COPY snapshot-dependencies/2 MByes, unless a snapshot changed
COPY application/0.4 MBno, rebuilt and pushed
Least changed first, most changed last: only the final small layer moves.

4Following One Commit Through the Build

Suppose we change a single line in OrderService and rebuild. Stage 1 runs again, because the jar changed, but that work is thrown away after the build. In stage 2, the base image and the user creation are cached. The dependencies directory has exactly the same content as before, so its COPY hits the cache, and so do the loader and snapshot layers. Only COPY application/ sees new content, so only that 400 KB layer is created. docker push then uploads 400 KB, and nodes that run the previous version pull 400 KB.

If we had copied application first and dependencies last, the same commit would invalidate the dependency layer too, because it comes after a changed layer, and we would be back to pushing 90 MB. That is the whole reason for ordering from least to most often changed. The same rule applies to anything we add, such as a JVM agent or a CA certificate: stable things go early.

5Running as a Non-Root User and Pinning the Base Image

By default, a container runs its process as root. If an attacker exploits the application, root inside the container makes escaping or damaging mounted volumes much easier. So we create a system user and group with no login and no home directory, and switch to it with USER app before the ENTRYPOINT. The application only needs to read its files and listen on 8080, which is above 1024, so it needs no privileges. On Kubernetes we back this up with runAsNonRoot: true in the security context.

Pinning means the build always uses a known base image. eclipse-temurin:25-jre-noble fixes the Java major version and the Ubuntu release, which is a sensible minimum; a bare latest tag would silently move us to another Java version one day. For fully reproducible builds we pin the digest as well, for example FROM eclipse-temurin:25-jre-noble@sha256:..., and let a tool such as Renovate or Dependabot open a pull request when a patched image is published, so we get security fixes deliberately and with a test run.

6The Entrypoint and Two Small Details

The entrypoint is in exec form, a JSON array, so java runs as process 1 and receives the SIGTERM that the platform sends on shutdown, which lets Spring Boot shut down gracefully. In shell form, ENTRYPOINT java -jar app.jar, a shell would be process 1 and might not forward the signal. -XX:MaxRAMPercentage=75 sizes the heap from the container's memory limit and leaves a quarter for non-heap memory such as metaspace and thread stacks.

Finally, a .dockerignore that excludes everything except target/*.jar keeps the build context small, so docker build does not send the whole source tree and the .git folder to the daemon each time.

7Follow-up questions

Why not build the jar inside the Dockerfile with a Maven stage?

We can, by adding a stage that runs mvn package, and it makes the build self-contained. The drawback is caching: dependency downloads must be cached carefully, with a separate COPY of pom.xml and dependency:go-offline or BuildKit cache mounts, or every build downloads the internet again. Many teams build the jar in CI, where tests run anyway, and keep the Dockerfile for packaging only.

Can we use a smaller base image, such as distroless?

Yes. A distroless Java image has no shell or package manager, which shrinks the image and the attack surface. The trade-off is debugging, since there is no shell to exec into, and the user setup must use the image's built-in non-root user instead of useradd. jlink can also build a custom minimal runtime for the application.

How do we add class data sharing to speed up start-up?

On the extracted layout we can run the application once during the image build with an option that writes a CDS archive, and then start with the archive in the entrypoint; recent Spring Boot and JDK versions document a training run for this, and JDK 25 adds an ahead-of-time cache. The exact flags differ by JDK version, so check your version before copying them.

8Conclusion

In this answer, we wrote a multi-stage Dockerfile whose first stage extracts the Boot jar into layers with java -Djarmode=tools -jar app.jar extract --layers, and whose second stage copies dependencies, spring-boot-loader, snapshot-dependencies and application in that order, from least to most often changed, so that a typical commit rebuilds and pushes only the small application layer. We ran the process as a non-root user, used an exec-form entrypoint with java -jar on the extracted jar, and pinned the base image by version and ideally by digest.

What to say in the interviewAbout 80 seconds aloud

Open with

I would use two stages. The first stage starts from a JRE image, copies the Boot jar in, and runs java -Djarmode=tools -jar app.jar extract --layers, which splits the jar into four directories using its layers index: dependencies, spring-boot-loader, snapshot-dependencies and application.

Then, if they want more

On Boot before 3.3 I would use the layertools jar mode instead. The second stage starts from a pinned JRE base, for example eclipse-temurin 25 JRE with a fixed OS release, and ideally pinned by digest with Renovate proposing updates.

It creates a system user, then copies the four directories in order: dependencies first, then the loader, then snapshot dependencies, and application last. The order is the important part. Docker rebuilds a layer and everything after it when its content changes, so I put the least frequently changed content first.

A normal code commit then reuses the ninety megabyte dependency layer from cache and only creates and pushes the application layer, which is usually under a megabyte.

Then USER switches to the non-root user, and the entrypoint is exec form, java with MaxRAMPercentage and -jar app.jar on the extracted application jar, so java is process 1 and receives SIGTERM for graceful shutdown. The first stage, with the original jar, is not part of the final image.

About this answer

Last updated
12 November 2026
Versions it names
Java 25
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
Spring Boot reference
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 "Write a multi-stage Dockerfile for a Boot application that uses layered jar extraction...": https://scrivio-interview-library.netlify.app/spring-boot/packaging-and-deployment/abd2ee60af/