Build Cache
4 min read Quiz at the end
Order Dockerfile layers from least to most changed and use BuildKit cache mounts for speed.
Build Cache Optimisation
# WRONG — busts cache on every code change
COPY . .
RUN pip install -r requirements.txt
# CORRECT — dependencies cached unless file changes
COPY requirements.txt .
RUN pip install -r requirements.txt
COPY . .
# BuildKit cache mount — persists across builds
RUN --mount=type=cache,target=/root/.cache/pip pip install -r requirements.txt
docker build --no-cache -t myapp .
docker build --cache-from myapp:cache -t myapp .
Topic Quiz · 2 questions
Test your understanding before moving on
1. Which Dockerfile pattern maximises layer caching for a Node.js app?
💡 Copying package files first lets the npm ci layer be cached until dependencies change.
2. What does docker build --no-cache do?
💡 --no-cache forces Docker to re-execute every layer even if nothing changed.