# base node image
FROM node:18-bullseye-slim as base
# set for base and all layers that inherit from it
ENV NODE_ENV production
# Install openssl for Prisma
RUN apt-get update && apt-get install -y openssl
FROM base as deps
WORKDIR /myapp
# Add package.json and yarn.lock
ADD package.json ./
ADD yarn.lock ./
# Install dependencies including dev dependencies
RUN yarn install --frozen-lockfile --dev
# Setup production node_modules
FROM base as production-deps
WORKDIR /myapp
# Copy dependencies from deps stage
COPY --from=deps /myapp/node_modules /myapp/node_modules
ADD package.json ./
ADD yarn.lock ./
# Install production dependencies
RUN yarn install --frozen-lockfile --production
# Build the app
FROM base as build
WORKDIR /myapp
# Copy dependencies and source code from deps stage
COPY --from=deps /myapp/node_modules /myapp/node_modules
ADD prisma .
# Generate Prisma client
RUN npx prisma generate
ADD . .
# Build the app and capture detailed logs
RUN yarn build
# Finally, build the production image with minimal footprint
FROM base
WORKDIR /myapp
# Copy production dependencies and built assets from previous stages
COPY --from=production-deps /myapp/node_modules /myapp/node_modules
COPY --from=build /myapp/node_modules/.prisma /myapp/node_modules/.prisma
COPY --from=build /myapp/build /myapp/build
COPY --from=build /myapp/public /myapp/public
ADD . .
CMD ["yarn", "start"]