This is similar to what is suggested here which says that there are no optional copy:
ONBUILD COPY package.json npm-shrinkwrap.json /usr/src/app/
This will fail for every project without an npm-shrinkwrap.json. This leaves us with three options:
Option 1: Copy everything before installing:
ONBUILD COPY . /usr/src/app/
ONBUILD RUN npm install
This will effectively disable Docker's build-cache and all modules will have to be re-installed for each change to the source code. The reason we use COPY package.json /usr/src/app/ is to only having to run npm install whenever package.json is changed.
Option 2: Make an :onbuild-shrinkwrap Image
ONBUILD COPY npm-shrinkwrap.json /usr/src/app/
ONBUILD RUN npm install
This will create more images that we will have to maintain and we already have quite a few:
5 supported versions x 4 tags = 20 different images
Option 3: Leave this up to those (few) who needs it
The onbuild image variant is ment as a quickly get started with Dockerizing an application, and is not recommended by Docker Inc. for long term / production usage. There is an open issue #66 to discourage excessive use of the onbuild image.