Interview Prep/Real-World Interview/Write-a-Dockerfile tasks

Write a multi-stage Dockerfile for a Java Spring Boot app built with Maven.

IntermediateHands-onWrite-a-Dockerfile tasks
Answer
FROM maven:3.9-eclipse-temurin-21 AS build
WORKDIR /src
COPY pom.xml .
RUN mvn -B -q dependency:go-offline
COPY src ./src
RUN mvn -B -q package -DskipTests

FROM eclipse-temurin:21-jre
WORKDIR /app
RUN useradd --system --uid 10001 spring
COPY --from=build /src/target/*.jar app.jar
USER 10001
EXPOSE 8080
ENTRYPOINT ["java", "-XX:MaxRAMPercentage=75", "-jar", "app.jar"]

Key trick → Caching dependency:go-offline on pom.xml alone avoids re-downloading the internet on every code change. MaxRAMPercentage sizes the heap from the container memory limit.

All Real-World Interview questions Roadmap: Interview readiness

More on Write-a-Dockerfile tasks