For my own project, the solution is to use docker BuildKit to first build all the workspace and then build a docker image for the project workspace reusing the previous built files.
In details you have copy in the docker file the top package.json with yarn lock and then cherrypicking the package.json of the needed workspace.
Then running a yarn install and a yarn build to get everything working.
Here is 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
is a private workspace that is a dependency of the server
workspace.