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.
More on Multi-stage builds, cache, tags and registries
CI rebuilds from scratch every run and takes 12 minutes. How do you bring it down?IntermediateYou tag every build `myapp:latest`. A rollback is requested. What happens?BeginnerHow do you promote an image from dev to prod registry without rebuilding?IntermediateDisk on the build server keeps filling up with images.BeginnerWhen do you use `--no-cache` and when is it a smell?BeginnerExplain a build where `COPY` always misses the cache even though code did not change.Intermediate