What is a multi-stage Docker build and why is it a CI best practice?
IntermediateConceptBuild Tools and Artifact Repositories
Answer
A multi-stage Dockerfile uses a builder stage with compilers and dev dependencies, then copies only the built output into a minimal runtime stage. The final image is smaller, has a smaller attack surface, and does not contain source or build tools.
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
RUN mvn -B -ntp dependency:go-offline
COPY src ./src
RUN mvn -B -ntp -DskipTests package
FROM eclipse-temurin:21-jre-alpine
COPY --from=build /src/target/app.jar /app/app.jar
USER 10001
ENTRYPOINT ["java","-jar","/app/app.jar"]
More on Build Tools and Artifact Repositories
What is Maven and what is its build lifecycle?BeginnerWhat is the difference between `mvn package`, `install` and `deploy`?BeginnerHow do Maven dependency scopes and transitive dependencies work?IntermediateWhat is `settings.xml` in Maven and how do CI builds use it?IntermediateWhat is the Maven Wrapper and why use it?BeginnerHow do you cache dependencies and speed up Maven builds in CI?Intermediate