Interview Prep/Docker/Build Performance & CI

How do you cache Maven/Gradle/Go modules without bloating the image?

IntermediateProductionBuild Performance & CI
Answer

Use cache mounts, not layers:

RUN --mount=type=cache,target=/root/.m2 mvn -B -DskipTests package
RUN --mount=type=cache,target=/go/pkg/mod --mount=type=cache,target=/root/.cache/go-build go build ./...

Because the cache lives outside layers, nothing is added to the image and it survives builder restarts as long as the builder volume does.

All Docker questions Roadmap: Docker & containers

More on Build Performance & CI