Writing a Dockerfile for Spring Boot
On This Lesson 4 topics
This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.
In this lesson
write a small, safe image for a Spring Boot app.
Planned topics
- A Dockerfile lists the build steps; each instruction makes a layer.
- Multi-stage build: stage one builds the JAR with the JDK; stage two copies only the JAR into a JRE image, so the final image has no build tools.
- Run as a non-root user:
USER shop. - Order instructions so rarely changing layers come first (dependencies before source) for better caching.
- Use a
.dockerignore, pin base image versions, and never put secrets in the image. - Verify the current Temurin/JRE image tags before recording.
Draft outline details
Demonstration
FROM eclipse-temurin:21-jdk AS build
WORKDIR /src
COPY . .
RUN ./mvnw -q package -DskipTests
FROM eclipse-temurin:21-jre
RUN useradd -r shop
USER shop
COPY --from=build /src/target/shop.jar /app/shop.jar
ENTRYPOINT ["java","-jar","/app/shop.jar"]Check your understanding
Why two stages?
Show answer
The final image keeps only the JAR and JRE, not the build tools.
Practice
Apply this lesson with the module's lab, exercises and quiz.
Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.