Docker and node_modules - put them in a layer, or a volume? Docker and node_modules - put them in a layer, or a volume? docker docker

Docker and node_modules - put them in a layer, or a volume?


Yes. Don't rebuild node_modules over and over again. Just stick them in a data container and mount it read only. You can have a central process rebuild node_modules now and then.

As an added benefit, you get a much more predictable build because you can enforce that everyone uses the same node modules. This is critical if you want to be sure that you actually test the same thing that you're planning to put in production.

Something like this (untested!):

docker build -t my/module-container - <<END_DOCKERFILEFROM busyboxRUN mkdir -p /usr/local/nodeVOLUME /usr/local/nodeEND_DOCKERFILEdocker run --name=module-container my/module-containerdocker run --rm --volumes-from=module-container \    -v package.json:/usr/local/node/package.json \    /bin/bash -c "cd /usr/local/node; npm install"

By now, the data container module-container will contain the modules specified by package.json in /usr/local/node/node_modules. It should now be possible to mount it in the production containers using --volume-from=module-container.