69 lines
2.1 KiB
Docker
69 lines
2.1 KiB
Docker
FROM node:22-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 openssl
|
|
WORKDIR /app
|
|
|
|
# Install dependencies based on the preferred package manager
|
|
COPY package.json package-lock.json* ./
|
|
COPY prisma ./prisma/
|
|
RUN npm install
|
|
|
|
# Rebuild the source code only when needed
|
|
FROM base AS builder
|
|
RUN apk add --no-cache openssl
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
# Environment variables must be present at build time
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy"
|
|
|
|
# Generate Prisma Client
|
|
RUN npx prisma generate
|
|
|
|
RUN npm run build
|
|
|
|
# Production image, copy all the files and run next
|
|
FROM base AS runner
|
|
RUN apk add --no-cache openssl
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV DATABASE_URL="postgresql://dummy:dummy@localhost:5432/dummy"
|
|
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nextjs
|
|
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# Set the correct permission for prerender cache and sqlite
|
|
RUN mkdir .next
|
|
RUN chown nextjs:nodejs /app .next
|
|
RUN npm install -g prisma@7.9.1
|
|
|
|
# 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 --chown=nextjs:nodejs /app/prisma ./prisma
|
|
COPY --from=builder --chown=nextjs:nodejs /app/prisma.config.ts ./prisma.config.ts
|
|
|
|
# Install modules that Next.js standalone tracing might miss
|
|
RUN npm install dotenv pg @prisma/adapter-pg
|
|
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
ENV PORT=3000
|
|
ENV HOSTNAME="0.0.0.0"
|
|
|
|
# We need to run migrations before starting the app.
|
|
# Since we are using PostgreSQL and might not have migration files, we use db push
|
|
CMD ["sh", "-c", "npx prisma db push --accept-data-loss && node server.js"]
|