A Story of Ludicrous Speedups (3000 sec -> 5 sec)
During a work hackathon, our project involved using Docker for deployment and dependency management.
The dockerfile was inherited from an underlying open source project and was ok when used for deployments but very slow for local development work. Why, you might ask?
It used multi-stage builds, one for node, one for golang and then a final stage that collected the built artifacts from the prior stages. But the problem was…
Conflating package installation with project build
The Dockerfile failed to use a best practice of first copying over the package manifest. For Node these are package.json & yarn.lock. For Golang it’s go.mod and go.sum.
Instead of copying over these specific files up front, the Dockerfile copied the full project into the container then performing a build.
The problem
Since the local copied source code changed frequently during development, all later steps in the Dockerfile were invalidated and performed without caching :(. Downloading all golang dependencies and compiling from scratch was onerous.
The solution
Break apart the dependency installation phase from the local code phase.
Package manifests should be copied in first, then yarn install
will
install Node dependencies. I had to get hacky to accomplish the same
thing with Golang, but I’ll post my solution when I have a good moment.
Conceptually, the outcome was:
- Build phase a1: Copy in package manifests for Node & yarn install
- Build phase b: Copy in package manifests & fetch golang deps
- Build phase a2 (built on a1): Build local code for js/ts
- Build phase b2 (built on b1): Build local code for golang
- Build phase c (independent of a or b) Selectively copy build artifacts from a2 and b2.
Outcome
Dev build time for docker image is now near instant (5 seconds) rather than 2900 seconds on a low power laptop.
Bonus
We also created a dedicated Dockerfile.dev that excluded js production build logic which was accounting for 300+ seconds of build time. Instead the js was built with a development script enabling hot module reloading.