Docker Layer Caching: How It Works and Optimization Tips
Fast recall
Quick revision
Core answer
Whether you understand why Docker builds are slow and how to structure Dockerfiles for fast incremental rebuilds
COPY . . as the first instruction — any file change invalidates the entire cache, forcing full dependency re-download on every build
Explain cache invalidation propagation (a miss invalidates all subsequent layers), discuss RUN --mount=type=cache for package managers, and mention BuildKit's parallel stage execution
Interview-ready guide
Interview answer
Docker builds images by executing instructions one at a time and caching each resulting layer. The critical rule: Docker invalidates every layer after the first change it detects — instruction order determines whether you get a cache hit or a full rebuild.
The ordering rule — dependencies before source code
- Copy and install dependencies before copying source code —
COPY pom.xml .thenRUN mvn dependency:go-offline, thenCOPY src ./srcensures a code-only change hits the cached dependency layer and skips the 3-minute download phase - Once any layer is invalidated, every subsequent layer rebuilds even if its content did not change — a source copy instruction placed before
RUN mvn dependency:go-offlinemeans every code commit triggers a full dependency re-download - For Node.js or Python:
COPY package.json .thenRUN npm installbeforeCOPY . .follows the same logic
`RUN` instruction consolidation
- Multiple
RUNinstructions each create a separate layer; combining cleanup in the sameRUNthat installs packages keeps the image smaller —RUN apt-get update && apt-get install -y curl && rm -rf /var/lib/apt/lists/*in one instruction prevents the package cache from persisting in an intermediate layer - Splitting an
apt-get updatefromapt-get installacross twoRUNinstructions causes the stale cache problem: Docker caches the update layer and uses the cached package list, then installs potentially outdated packages on the next build - Don't consolidate instructions that change frequently with instructions that change rarely — a
RUNthat sets up the OS and installs your application JAR will both re-execute on every JAR change
BuildKit and remote caching
- Enable BuildKit with
DOCKER_BUILDKIT=1or usedocker buildx build— BuildKit parallelizes independent build stages and provides more granular cache invalidation than the classic builder --cache-frompulls a previous build's layers from a registry for use as cache — CI agents without a local build cache can still achieve cache hits by pulling the most recent image from the registry before building
Docker Layer Caching: How It Works and Optimization Tips
Direct Answer
Docker caches each instruction's result as a layer. A cache miss invalidates all subsequent layers. Optimize by ordering instructions from least-to-most frequently changing: COPY pom.xml first and run dependency download, then COPY src/ last. This ensures dependency layers are reused across most builds, and only source code layers rebuild on code changes.
Common Mistakes & Pitfalls
COPY . . as the first instruction — any file change invalidates the entire cache, forcing full dependency re-download on every build
Check your understanding
Answer these without looking back at the article.
- What is BuildKit and how does it improve over the classic Docker builder?
- How does RUN --mount=type=cache differ from a named volume in a docker-compose file?
- Why does changing a file's permissions (chmod) in a Dockerfile create a new layer?
Related interview prep categories
2Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.