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 →
- Order the Dockerfile so dependencies install before the source code is copied.
- 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.
More on Pipelines at scale
The CI pipeline takes 45 minutes. Developers want it under 10. How do you approach it?IntermediateYou have a monorepo with 40 services. Every commit builds and tests all of them. How do you fix that?AdvancedFlaky tests fail 1 in 5 runs and developers just click "re-run". How do you fix this culture and the tests?BeginnerYour self-hosted GitHub runners can't keep up at peak and sit idle at night. How do you run them at scale?AdvancedHow do you secure the software supply chain from commit to production?AdvancedFifty teams have copy-pasted pipelines that have drifted apart. How do you standardise them?Intermediate