Docker Image Layer Caching
How do Docker image layers affect build time, and how do you order a Dockerfile for better cache hits?
Answers use simple, clear English.
Quick interview answer
Each instruction creates a layer hashed by content. Put rarely changing steps first (deps install) and frequently changing (COPY source) later so dependency layers cache. Use .dockerignore, multi-stage builds, and pin base digests for reproducibility.
Detailed answer
Each instruction creates a layer hashed by content. Put rarely changing steps first (deps install) and frequently changing (COPY source) later so dependency layers cache. Use .dockerignore, multi-stage builds, and pin base digests for reproducibility.
Real example & use case
CI rebuilds only app layers when package-lock.json unchanged, cutting Node image build from minutes to seconds.
Pros & cons
Pros: fast incremental builds. Cons: accidental COPY . early busts cache every commit.