diff --git a/Dockerfile b/Dockerfile index d20a46b..4ae4964 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,15 +1,15 @@ +# To use this Dockerfile, you have to set `output: 'standalone'` in your next.config.js file. + +FROM node:20-alpine AS base + # Install dependencies only when needed FROM base AS deps RUN apk add --no-cache libc6-compat WORKDIR /app -COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ -RUN \ - if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ - elif [ -f package-lock.json ]; then npm ci; \ - elif [ -f pnpm-lock.yaml ]; then corepack enable && corepack prepare pnpm@10.11.0 --activate && pnpm i --frozen-lockfile; \ - else echo "Lockfile not found." && exit 1; \ - fi +# Copy lockfile and install deps with pnpm +COPY package.json pnpm-lock.yaml ./ +RUN corepack enable && corepack prepare pnpm@10.11.0 --activate && pnpm i --frozen-lockfile # Rebuild the source code only when needed FROM base AS builder @@ -17,9 +17,30 @@ WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . -RUN \ - if [ -f yarn.lock ]; then yarn run build; \ - elif [ -f package-lock.json ]; then npm run build; \ - elif [ -f pnpm-lock.yaml ]; then corepack enable && corepack prepare pnpm@10.11.0 --activate && pnpm run build; \ - else echo "Lockfile not found." && exit 1; \ - fi \ No newline at end of file +# Enable corepack again for build stage +RUN corepack enable && corepack prepare pnpm@10.11.0 --activate && pnpm run build + +# Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production + +RUN addgroup --system --gid 1001 nodejs +RUN adduser --system --uid 1001 nextjs + +COPY --from=builder /app/public ./public + +RUN mkdir .next +RUN chown nextjs:nodejs .next + +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs + +EXPOSE 3000 +ENV PORT=3000 +ENV HOSTNAME="0.0.0.0" + +CMD ["node", "server.js"] \ No newline at end of file