title: How to cache node_modules on Docker build?
body: <p>I've been trying for some time to cache <code>node_modules</code> on a Docker build. I've tried several approaches including the one <a href="http://bitjudo.com/blog/2014/03/13/building-efficient-dockerfiles-node-dot-js/" rel="noreferrer">here</a>, but without success.</p>
  
  <p>My main reason to cache is because it takes <strong>30+</strong> minutes to build my image, which is way too much.</p>
  
  <p>My <code>Dockerfile</code>:</p>
  
  <pre><code># This image will be based on the oficial nodejs docker image
  FROM node:4.2.1
  
  RUN npm install -g jspm@0.17.0-beta.7 &amp;&amp; \
      npm install -g gulp &amp;&amp; \
      npm install -g tsd
  
  # Use changes to package.json to force Docker not to use the cache
  # when we change our application's nodejs dependencies:
  ADD package.json /src/package.json
  RUN cd /src &amp;&amp; npm install
  
  # Put all our code inside that directory that lives in the container
  ADD . /src
  
  # Set in what directory commands will run
  WORKDIR /src
  
  # Install dependencies
  RUN cd /src &amp;&amp; \
      tsd reinstall -so &amp;&amp; \
      jspm install &amp;&amp; \
      gulp build -p
  
  # Tell Docker we are going to use this port
  EXPOSE 3000
  
  # The command to run our app when the container is run
  CMD ["npm", "run", "start-production"]
  </code></pre>
  
  <p>I do not have a <code>.dockerignore</code> file. I added one before but it still didn't cache my <code>node_modules</code>.</p>
  
  <p>So, <strong>how to I cache my node_modules</strong>? Feel free to suggest modifications to the <code>Dockerfile</code>.</p>
  
  <p>Thanks!</p>