title: how to organize shared nodejs libraries with docker and monorepo using multi-stage build
body: <p>My question is very similar to this on:</p>
  <ul>
  <li><a href="https://stackoverflow.com/questions/60401518/how-to-organize-shared-libraries-with-docker-and-monorepo">How to organize shared libraries with docker and monorepo</a>
  and</li>
  <li><a href="https://stackoverflow.com/questions/64012316/git-monorepo-layout-with-shared-library">Git monorepo layout with shared library</a> .</li>
  </ul>
  <p>Neither of these threads has arrived at a solution. Then I found this:
  <a href="https://medium.com/@xfor/yarn-workspaces-and-docker-39e30402b69b" rel="nofollow noreferrer">https://medium.com/@xfor/yarn-workspaces-and-docker-39e30402b69b</a> which advises that a multi-stage build be used like so:</p>
  <pre><code>FROM node:10-alpine as build
  
  WORKDIR /usr/src/app
  
  COPY package.json .
  COPY yarn.lock .
  COPY packages/shared ./packages/shared
  COPY packages/api ./packages/api
  
  RUN yarn install --pure-lockfile --non-interactive
  
  WORKDIR /usr/src/app/packages/shared
  RUN yarn build
  
  WORKDIR /usr/src/app/packages/api
  RUN yarn build
  
  FROM node:10-alpine
  
  WORKDIR /usr/src/app
  
  COPY package.json .
  COPY yarn.lock .
  
  COPY --from=build /usr/src/app/packages/shared/package.json /usr/src/app/packages/shared/package.json
  COPY --from=build /usr/src/app/packages/shared/dist /usr/src/app/packages/shared/dist
  
  COPY --from=build /usr/src/app/packages/api/package.json /usr/src/app/packages/api/package.json
  COPY --from=build /usr/src/app/packages/api/build /usr/src/app/packages/api/build
  
  ENV NODE_ENV production
  
  RUN yarn install --pure-lockfile --non-interactive --production
  
  WORKDIR /usr/src/app/packages/api
  
  CMD [&quot;npm&quot;, &quot;start&quot;]
  </code></pre>
  <p>Unfortunately, I don't use yarn for managing my monorepo and my Dockerfile is located within the app level of the monorepo rather than at the root of the monorepo. I am using jazelle to manage my monorepo and was wondering if I would have to create a <code>Dockerfile</code> for each app at the root level.</p>