title: Speed up NPM install in Docker container
body: <p>We use the standard practices of not including node_modules in version control. However, when moving through the CI/CD pipeline, we have to reinstall NPM dependencies in several places and it makes everything very slow. </p>
  
  <p>Is there a way to somehow cache NPM dependencies with Docker? I searched Google "docker cache npm dependencies" and the first hit from 2014 yielded a dead link.</p>
  
  <p>Nothing else of much value came up.</p>
  
  <p>One solution is to include node_modules in version control, but I think that would be a huge mistake. I think caching the dependencies somehow would be the best option.</p>
  
  <p>Here is the Dockerfile as is:</p>
  
  <pre><code>FROM node:6
  
  COPY . .  # copy all files, but node_modules does not exist ( =&gt; gitignored)
  
  RUN npm install --no-optional --only=production &gt; /dev/null 2&gt;&amp;1
  RUN npm install -g bower  &gt; /dev/null 2&gt;&amp;1
  RUN bower install --config.interactive=false --allow-root &gt; /dev/null 2&gt;&amp;1
  
  ENTRYPOINT ["/bin/bash", "/root/cdt/run.sh"]
  </code></pre>
  
  <p>Here is one possible solution, but I can't quite figure out how it works:</p>
  
  <p>=> <a href="http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/" rel="noreferrer">http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/</a></p>