title: Artifact caching for multistage docker builds
body: <p>I have a Dockerfiles like this </p>
  
  <pre><code># build-home
  FROM node:10 AS build-home
  WORKDIR /usr/src/app
  COPY /home/package.json /home/yarn.lock /usr/src/app/
  RUN yarn install
  COPY ./home ./
  RUN yarn build
  
  # build-dashboard
  FROM node:10 AS build-dashboard
  WORKDIR /usr/src/app
  COPY /dashboard/package.json /dashboard/yarn.lock /usr/src/app/
  RUN yarn install
  COPY ./dashboard ./
  RUN yarn build
  
  # run
  FROM nginx
  EXPOSE 80
  COPY nginx.conf /etc/nginx/nginx.conf
  COPY --from=build-home /usr/src/app/dist /usr/share/nginx/html/home
  COPY --from=build-dashboard /usr/src/app/dist /usr/share/nginx/html/dashboard
  </code></pre>
  
  <p>Here building two react application and then artifacts of build are put in nginx. To improve build performance, I need to cache the <code>dist</code> folder in the <code>build-home</code> and<code>build-dashboard</code> build-stages.
  For this i create a <code>volume</code> in <code>docker-compose.yml</code></p>
  
  <pre><code>...
    web:
      container_name: web
      build:
        context: ./web
      volumes:
        - ./web-build-cache:/usr/src/app
      ports:
        - 80:80
      depends_on:
        - api
  </code></pre>
  
  <p>I’ve stopped at this stage because I don’t understand how to add <code>volume</code> created by<code>docker-compose</code> first for the <code>build-home</code> stage, and after adding this<code>volume</code> to the <code>build-dashboard</code>. 
  Maybe i should be create a two volumes and attach each to each of build stages, but how do this?</p>
  
  <p><strong>UPDATE:</strong></p>
  
  <p><a href="https://pastebin.com/JNnrxD5F" rel="nofollow noreferrer">Initial build.</a></p>
  
  <p>Home application: </p>
  
  <ol>
  <li>Install modules: 100.91s</li>
  <li>Build app: 39.51s</li>
  </ol>
  
  <p>Dashboard application: </p>
  
  <ol>
  <li>Install modules: 100.91s</li>
  <li>Build app: 50.38s</li>
  </ol>
  
  <p>Overall time: </p>
  
  <pre><code>real    8m14.322s
  user    0m0.560s
  sys     0m0.373s
  </code></pre>
  
  <p><a href="https://pastebin.com/KSKKe4y3" rel="nofollow noreferrer">Second build</a> (without code or dependencies change):</p>
  
  <p>Home application: </p>
  
  <ol>
  <li>Install modules: Using cache</li>
  <li>Build app: Using cache</li>
  </ol>
  
  <p>Dashboard application: </p>
  
  <ol>
  <li>Install modules: Using cache</li>
  <li>Build app: Using cache</li>
  </ol>
  
  <p>Overall time: </p>
  
  <pre><code>real    0m2.933s
  user    0m0.309s
  sys     0m0.427s
  </code></pre>
  
  <p><a href="https://pastebin.com/QxazgmUE" rel="nofollow noreferrer">Third build</a> (with small change in code in first app):</p>
  
  <p>Home application: </p>
  
  <ol>
  <li>Install modules: Using cache</li>
  <li>Build app: 50.04s</li>
  </ol>
  
  <p>Dashboard application: </p>
  
  <ol>
  <li>Install modules: Using cache</li>
  <li>Build app: Using cache</li>
  </ol>
  
  <p>Overall time: </p>
  
  <pre><code>real    0m58.216s
  user    0m0.340s
  sys     0m0.445s
  </code></pre>
  
  <p><a href="https://pastebin.com/p1p0uTvS" rel="nofollow noreferrer">Initial build</a> of home application without Docker: 89.69s</p>
  
  <pre><code>real    1m30.111s
  user    2m6.148s
  sys     2m17.094s
  </code></pre>
  
  <p><a href="https://pastebin.com/aFJB7ZKs" rel="nofollow noreferrer">Second build</a> of home application without Docker, the <code>dist</code> folder exists on disk (without code or dependencies change): 18.16s</p>
  
  <pre><code>real    0m18.594s
  user    0m20.940s
  sys     0m2.155s
  </code></pre>
  
  <p><a href="https://pastebin.com/M03repAZ" rel="nofollow noreferrer">Third build</a> of home application without Docker, the <code>dist</code> folder exists on disk (with small change in code): 20.44s</p>
  
  <pre><code>real    0m20.886s
  user    0m22.472s
  sys     0m2.607s
  </code></pre>
  
  <p>In the docker-container, the third builds of the application is 2 times longer. This shows that if the result of the first build is on disk, other builds completed faster. In the docker container, all assemblies after the first are executed as long as the first, because there is no <code>dist</code> folder.</p>