Interview Prep/CI/CD/Build Tools and Artifact Repositories

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"]
All CI/CD questions Roadmap: CI/CD fundamentals

More on Build Tools and Artifact Repositories