FROM node:16-alpine AS runner
ARG IMAGE_TAG_ARG='m-default'
WORKDIR /app

COPY ./.yarn/plugins ./.yarn/plugins
COPY ./.yarn/releases ./.yarn/releases
COPY ./.yarn/sdks ./.yarn/sdks
COPY ./.yarn/patches ./.yarn/patches

COPY ./.pnp.cjs ./.pnp.loader.mjs ./
COPY *.json yarn.lock .yarnrc.yml ./

COPY ./packages/ ./packages/
COPY ./sites/client/next.config.js ./sites/client/next.config.js
COPY ./sites/client/public ./sites/client/public
COPY ./sites/client/package.json ./sites/client/package.json

# only install dependency in production
RUN yarn workspaces focus --production @sites/client


COPY ./sites/client/.next/static ./sites/client/.next/static
COPY ./sites/client/.next/server ./sites/client/.next/server
COPY ./sites/client/.next/*.json ./sites/client/.next/BUILD_ID ./sites/client/.next/
COPY ./sites/client/dist ./sites/client/dist

ENV NEXT_PUBLIC_IMAGE_TAG=${IMAGE_TAG_ARG}
ENV NODE_ENV=production
ENV NODE_OPTIONS='--require /app/.pnp.cjs'
ENV PORT=3000
EXPOSE 3000
USER node

CMD [ "yarn", "start:client" ]