To solve the challenges in my project, I have implemented a strategy using docker BuildKit. This involves first building all the workspace components and then creating a docker image for the project workspace while reusing previously built files.
The process includes copying the top package.json along with the yarn lock file in the dockerfile, followed by selecting the package.json specific to the required workspace. Subsequently, running commands for yarn install and yarn build to ensure smooth functionality.
Below is the outline of my project:
# base image
FROM @myscope/base:latest as base
# set working directory
WORKDIR /app
# add `/app/node_modules/.bin` to $PATH
ENV PATH /app/node_modules/.bin:$PATH
# install and cache app dependencies
COPY ["package.json","yarn.lock", "./"]
COPY ./packages/server/package.json ./packages/server/
COPY ./packages/shared/package.json ./packages/shared/
COPY ./packages/app/package.json ./packages/app/
RUN yarn install --frozen-lockfile --non-interactive --production=false --ignore-scripts
COPY . /app
RUN yarn build
FROM nodejs:14.15 as serverapp
WORKDIR /app
COPY ["package.json","yarn.lock", "./"]
COPY ./packages/server/package.json ./packages/server/
COPY ./packages/shared/package.json ./packages/shared/
RUN yarn install --frozen-lockfile --non-interactive --production=true --ignore-scripts
# copy artifact build from the 'build environment'
COPY --from=base /app/packages/shared/dist /app/packages/shared/dist
COPY ["./packages/server/", "./packages/server/"]
WORKDIR /app/packages/server
VOLUME ["/app/packages/server/logs", "/app/packages/server/uploads"]
EXPOSE $PORT
CMD ["yarn", "start"]
shared
denotes a private workspace that serves as a dependency for the server
workspace.