# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file.
# From https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile

FROM oven/bun:1-alpine AS base

# Install dependencies only when needed
FROM base AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app

ARG FONTAWESOME_PACKAGE_TOKEN

# Install dependencies based on the preferred package manager
COPY package.json bun.lock* ./
RUN \
  if [ -n "$FONTAWESOME_PACKAGE_TOKEN" ]; then \
    printf "@fortawesome:registry=https://npm.fontawesome.com/\n@awesome.me:registry=https://npm.fontawesome.com/\n//npm.fontawesome.com/:_authToken=%s\n" "$FONTAWESOME_PACKAGE_TOKEN" > .npmrc; \
  fi; \
  bun install --frozen-lockfile; \
  rm -f .npmrc

# Copy package files and install dependencies with Bun
COPY package.json bun.lockb ./
RUN bun install --frozen-lockfile

FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules

# Copy the rest of the app source and build it
COPY . .
RUN bun run build

# Stage 2: The Runner (using Node)
FROM node:20-alpine as runner
WORKDIR /app

ENV NODE_ENV production

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

# Only copy what's necessary for production from the builder stage
COPY --from=base /app/.next ./.next
COPY --from=base /app/node_modules ./node_modules
COPY --from=base /app/package.json ./package.json
COPY --from=base /app/public ./public

# Expose port and define command to run the app with Node
EXPOSE 3000
CMD ["node_modules/.bin/next", "start"]