Guides
How to optimise a Dockerfile for CI
Small code changes trigger long rebuilds.
6 min read
Small code changes trigger long rebuilds.
Why it happens
Instruction order defeats the layer cache, and the build context is larger than it needs to be.
How to fix it
- Copy the lockfile, install dependencies, then copy source — in that order
- Add a .dockerignore covering .git, node_modules and build output
- Use multi-stage builds so build tooling stays out of the final image
- Use cache mounts for package manager directories rather than caching layers
Your next build could be twice as fast, at half the price
Start free. Migrating away is the same one line, and we publish that diff too.
Related
How to set up Docker layer cachingDocker builds take ten minutes because every layer rebuilds.How to use multi-stage Docker buildsImages are large and contain build tooling that should not ship.How to run concurrent Docker builds safelyParallel matrix legs contend and builds slow down or fail.How to use a remote Docker builderThe layer cache is transferred to and from the runner on every build.