A Dockerfile copies the whole repository, then runs `./mvnw package`, and every build re-downloads every dependency even when only a controller class changed. A teammate says "just add more RAM to the build machine." What's actually wrong, and what's the fix?
Every instruction in a Dockerfile that touches the filesystem produces a layer, and Docker reuses a layer from cache only if that exact instruction's inputs are unchanged. COPY . /app copies the entire repository as one step, so a one-line change anywhere in the source invalidates that layer — and once one layer misses the cache, every layer after it is rebuilt too, including the dependency download that follows it. More RAM doesn't touch this at all; the problem is which files are copied in which order, not how much memory the build has. The fix is to copy only the dependency manifest (pom.xml, the wrapper) first, run the dependency download against just that, and only then copy the actual source — so a source-only change invalidates the source and build layers but leaves the dependency layer, and its slow download, untouched.