Optimizing Dockerfile caching for NPM

In order for the Docker build cache to cache packages installed via npm, it’s necessary to add the package.json file to the image, and run npm install, before adding the rest of the repo (via ADD .).

Here’s an example of how that might look in a Dockerfile:

FROM node

# If needed, install system dependencies here

# Add package.json before rest of repo for caching
ADD package.json /app/
WORKDIR /app
RUN npm install

ADD . /app

# If needed, add additional RUN commands here