Final image still contains Maven and the full JDK despite 'minimizing' it
A single-stage Dockerfile does FROM maven:3.9-eclipse-temurin-21, builds the jar, then RUN rm -rf ~/.m2 to 'clean up'. The image is still 700MB+. Explain why and the correct fix. Expected reasoning: deleting files in a later RUN only adds a deletion marker layer — the earlier layer containing Maven, the JDK, and the dependency cache is still part of the image's layer history and still counted in size. The correct fix is a multi-stage build: build in the maven image, then COPY --from=builder only the resulting jar into a clean eclipse-temurin:21-jre-alpine final stage.
Ready to master this question?
Generate a complete walkthrough — background, the full answer in plain language, a working code example explained line by line, a real-world scenario, common mistakes, and how this same question gets asked in different ways.
Sign in to generate a response