Interview Prep/Docker/Multi-stage builds, cache, tags and registries

Show a multi-stage Dockerfile for a Java Spring Boot app and explain what you gain.

BeginnerScenarioMulti-stage builds, cache, tags and registries
Answer

Situation read → JDK and Maven are needed to build, only a JRE is needed to run.

What I'd do →

FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
RUN --mount=type=cache,target=/root/.m2 mvn -q dependency:go-offline
COPY src ./src
RUN --mount=type=cache,target=/root/.m2 mvn -q package -DskipTests

FROM eclipse-temurin:21-jre
COPY --from=build /src/target/app.jar /app.jar
USER 10001
ENTRYPOINT ["java","-jar","/app.jar"]

Why → Final image carries only the JRE and jar, usually 250 MB instead of 700 MB+.

Interview tip → Mention the Maven cache mount for fast rebuilds.

All Docker questions Roadmap: Docker & containers

More on Multi-stage builds, cache, tags and registries