Docker builds in CI take 12 minutes because every runner starts with an empty cache. How do you speed them up?

IntermediateScenarioPipelines at scale
Answer

Situation read → Ephemeral runners lose the local layer cache. You need to store the cache somewhere persistent and order the Dockerfile so it's reused.

What I'd do →

  1. Order the Dockerfile so dependencies install before the source code is copied.
  2. Export BuildKit cache to a persistent backend:
- uses: docker/build-push-action@v6
  with:
    push: true
    tags: ${{ env.ECR }}/api:${{ github.sha }}
    cache-from: type=gha
    cache-to: type=gha,mode=max

(or type=registry pointing to an ECR cache image for large layers). 3. Use cache mounts for package managers (RUN --mount=type=cache,target=/root/.npm npm ci). 4. Add a .dockerignore so unrelated file changes don't invalidate the build context. 5. For very large builds, a shared remote BuildKit builder keeps a warm cache on disk.

Why → Cache reuse turns a full rebuild into rebuilding only the layers that actually changed.

Interview tip → Explain mode=max: it caches intermediate stages too, which matters for multi-stage builds.

All Real-World Interview questions Roadmap: Interview readiness

More on Pipelines at scale