FROM node:20-alpine AS builder

RUN apk add --no-cache yarn

WORKDIR /app

COPY package.json yarn.lock ./

RUN yarn install --frozen-lockfile sharp

COPY . .
COPY ./src/app/favicon.ico ./public/favicon.ico

RUN yarn build

FROM node:20-alpine AS runner

WORKDIR /app

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 --ingroup nodejs nextjs

RUN mkdir -p ./.next/cache
RUN chown nextjs:nodejs ./.next/cache

ENV NODE_ENV=production

USER nextjs

COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

EXPOSE 3000

CMD ["node", "server.js"]