0
0
Spring Bootframework~10 mins

Multi-stage Docker builds in Spring Boot - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Multi-stage Docker builds
Start: Base Build Stage
Compile & Package App
Create Smaller Runtime Stage
Copy Artifacts from Build Stage
Set Runtime Environment
Build Final Image
Run Container with Final Image
The Dockerfile first builds the app in one stage, then copies only the needed files to a smaller image for running.
Execution Sample
Spring Boot
FROM maven:3.8.7-openjdk-17 AS build
WORKDIR /app
COPY pom.xml .
COPY src ./src
RUN mvn clean package -DskipTests

FROM openjdk:17-jdk-slim
COPY --from=build /app/target/*.jar /app/app.jar
ENTRYPOINT ["java", "-jar", "/app/app.jar"]
This Dockerfile builds a Spring Boot app with Maven, then creates a smaller image with just the jar to run.
Execution Table
StepActionStageFiles PresentResult
1Start build stage with Maven imagebuildBase image ready
2Copy pom.xmlbuildpom.xmlpom.xml copied
3Copy src directorybuildpom.xml, src/Source code copied
4Run mvn clean packagebuildpom.xml, src/, target/app.jarJar file created
5Start runtime stage with OpenJDK slimruntimeClean runtime image
6Copy jar from build stageruntimeapp.jarJar copied to runtime
7Set ENTRYPOINT to run jarruntimeapp.jarRuntime ready to start app
8Build final imagefinalapp.jarFinal image built with only runtime files
9Run container from final imagefinalapp.jarApp runs with minimal image
💡 All build steps complete; final image contains only runtime essentials.
Variable Tracker
VariableStartAfter Step 4After Step 6Final
pom.xmlabsentpresentabsentabsent
src/absentpresentabsentabsent
target/app.jarabsentpresentabsentabsent
app.jar in runtimeabsentabsentpresentpresent
Key Moments - 3 Insights
Why is the source code not present in the final image?
Because the source code is copied only in the build stage (see step 3) and not copied to the runtime stage (step 6 copies only the jar). This keeps the final image small.
How does the final image get the jar file without rebuilding?
The final image copies the jar from the build stage using the --from=build syntax (step 6), so it reuses the built artifact without recompiling.
Why use two different base images?
The build stage uses a Maven image with tools to compile the app, while the runtime stage uses a smaller OpenJDK image to run the app efficiently (steps 1 and 5).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, at which step is the jar file created?
AStep 2
BStep 6
CStep 4
DStep 8
💡 Hint
Check the 'Result' column for when 'Jar file created' appears.
At which step does the runtime stage start?
AStep 1
BStep 5
CStep 7
DStep 9
💡 Hint
Look for the step mentioning 'Start runtime stage with OpenJDK slim'.
If we copied the src folder to the runtime stage, what would happen?
AThe final image size would increase
BThe jar file would be rebuilt automatically
CThe app would run faster
DThe build stage would fail
💡 Hint
Refer to variable_tracker showing src/ presence only in build stage.
Concept Snapshot
Multi-stage Docker builds use multiple FROM lines.
First stage builds the app with all tools.
Second stage copies only needed files (like jar).
Final image is smaller and cleaner.
Use --from=stage to copy artifacts.
Improves build efficiency and image size.
Full Transcript
Multi-stage Docker builds start by using a base image with build tools to compile the Spring Boot app. The source code and pom.xml are copied into this build stage. Then the app is compiled with Maven, producing a jar file. Next, a second stage uses a smaller runtime image. It copies only the jar file from the build stage. This keeps the final image small and efficient. The ENTRYPOINT is set to run the jar. The container runs the app with minimal files. This process separates building and running, improving speed and size.