diff --git a/Dockerfile b/Dockerfile index 5d97133..05edd57 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,6 +1,7 @@ +ARG RUNNER_ENV=production + FROM node:20-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 @@ -15,9 +16,12 @@ RUN \ else echo "Lockfile not found." && exit 1; \ fi +FROM base AS builder-production + +ENV DATABASE_URL=${DATABASE_URL} +ENV NEXTAUTH_URL=${NEXTAUTH_URL} +ENV JWT_SECRET=${JWT_SECRET} -# Rebuild the source code only when needed -FROM base AS builder WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . @@ -35,7 +39,7 @@ RUN \ fi # Production image, copy all the files and run next -FROM base AS runner +FROM base AS runner-production WORKDIR /app ENV NODE_ENV production @@ -45,7 +49,7 @@ ENV NODE_ENV production RUN addgroup --system --gid 1001 nodejs RUN adduser --system --uid 1001 nextjs -COPY --from=builder /app/public ./public +COPY --from=builder-production /app/public ./public # Set the correct permission for prerender cache RUN mkdir .next @@ -53,8 +57,8 @@ RUN chown nextjs:nodejs .next # Automatically leverage output traces to reduce image size # https://nextjs.org/docs/advanced-features/output-file-tracing -COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ -COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static +COPY --from=builder-production --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder-production --chown=nextjs:nodejs /app/.next/static ./.next/static USER nextjs @@ -67,3 +71,8 @@ ENV HOSTNAME "0.0.0.0" # server.js is created by next build from the standalone output # https://nextjs.org/docs/pages/api-reference/next-config-js/output CMD ["node", "server.js"] + +############################################# +# Branch based on RUNNER_ENV for deployment # +############################################# +FROM runner-${RUNNER_ENV} AS runner