Explain Docker layer caching and how it impacts build speed.

Medium Topic: Docker May 24, 2026

Docker builds images layer by layer. If a layer hasn’t changed since the last build, Docker reuses the cached version. The trick is layer ordering:

Bad: COPY all files first, then run npm install. Any code change invalidates the npm install cache.

Good: COPY package.json first, run npm install, then COPY the rest of the source. Dependency installation only re-runs when package.json changes.

# Optimized layer order
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build
← Previous How do you reduce Docker image size? Walk... Next → How do you scan Docker images for vulnerabilities...

Practice Similar Questions

Back to Docker Topics