Optimizing Nodejs Docker build times — Layering explained.

Layering, is a core feature of docker. Basically, the idea is to cache as much as possible.
Lets take a basic example:
- Create an image based off of, alpine.
- Set working directory
- Copy all files
- Install dependencies
- Run the application.
All fairly basic and straight forward.
Now.. the way layering works is that it essentially caches the “output” of each line of your docker file. When rebuilding images, docker uses the cached “output” if no change has happened.
The way that the layering cache works is that if a change occured in the output of one of the lines in the dockerfile, we would have to ‘rerun’ subsequent lines.
Example: if we changed a source file (part of the “output” of COPY . .
we would have to rerun RUN yarn install
which can be a fairly expensive operation.
So what can we do to optimize this?
What if we refactor to copy in package.json
and install the dependencies, before copying the source?
This means that if we make a change to the sourcefiles, we wont actually need to reinstall all of the dependencies, because they will have been cached.