Docker builds take 12 minutes even when only one line of code changed. Speed them up.
IntermediateScenarioSpeed, Caching & Cost
Answer
Situation read → No layer cache on ephemeral runners, likely poor Dockerfile ordering.
What I'd do →
- Fix Dockerfile order: copy lockfile, install, then copy source.
- Use BuildKit with the GHA cache:
- uses: docker/setup-buildx-action@v3 - uses: docker/build-push-action@v6 with: cache-from: type=gha,scope=api cache-to: type=gha,mode=max,scope=api - Use
RUN --mount=type=cache,target=/root/.npm npm cifor package caches inside the build. - For images over a few GB, use
type=registrycache in ECR instead to avoid evicting the repo's other caches.
Why → Each hosted job starts with an empty Docker daemon; without an exported cache nothing is reused.
Interview tip → Mention mode=max for multi-stage builds.
More on Speed, Caching & Cost
`npm ci` takes 4 minutes on every run. Fix it.BeginnerThe repo hits the 10 GB cache limit daily and hit rates are poor. What do you change?IntermediatemacOS jobs are 60% of your bill. What are your options?IntermediateA job sometimes hangs and burns 6 hours. How do you stop it happening again?BeginnerYour PR matrix is 3 OS times 4 versions (12 jobs). How do you cut cost without losing coverage?AdvancedTest suite takes 30 minutes on one runner. How do you parallelize it in Actions?Intermediate