My question is very similar to this on:
- How to organize shared libraries with docker and monorepo and
- Git monorepo layout with shared library .
Neither of these threads has arrived at a solution. Then I found this: https://medium.com/@xfor/yarn-workspaces-and-docker-39e30402b69b which advises that a multi-stage build be used like so:
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 ["npm", "start"]
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 Dockerfile
for each app at the root level.