68 lines
1.7 KiB
Docker
68 lines
1.7 KiB
Docker
# Stage 1: Install dependencies
|
|
FROM node:20-alpine AS deps
|
|
RUN apk add --no-cache libc6-compat
|
|
WORKDIR /app
|
|
|
|
# Install dependencies based on the preferred package manager
|
|
COPY package.json package-lock.json* ./
|
|
RUN npm ci
|
|
|
|
# Stage 2: Rebuild the source code only when needed
|
|
FROM node:20-alpine AS builder
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
RUN npx prisma generate
|
|
RUN npm run build
|
|
|
|
# Stage 3: Production image, copy all the files and run next
|
|
FROM node:20-alpine AS runner
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV production
|
|
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nextjs
|
|
|
|
# Copy public folder
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# Set the correct permission for prerender cache
|
|
RUN mkdir .next
|
|
RUN chown nextjs:nodejs .next
|
|
|
|
# Copy standalone build
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
|
# Copy Prisma files for migrations (as root before switching user)
|
|
COPY --from=builder /app/prisma ./prisma
|
|
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
|
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma
|
|
COPY --from=builder /app/node_modules/prisma ./node_modules/prisma
|
|
|
|
# Fix permissions for prisma folder
|
|
RUN chown -R nextjs:nodejs ./prisma ./node_modules/.prisma ./node_modules/@prisma ./node_modules/prisma
|
|
|
|
# Create entrypoint script
|
|
COPY <<EOF /app/entrypoint.sh
|
|
#!/bin/sh
|
|
set -e
|
|
echo "Running database migrations..."
|
|
./node_modules/.bin/prisma migrate deploy
|
|
echo "Migrations completed. Starting application..."
|
|
exec node server.js
|
|
EOF
|
|
|
|
RUN chmod +x /app/entrypoint.sh
|
|
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
ENV PORT 3000
|
|
ENV HOSTNAME "0.0.0.0"
|
|
|
|
CMD ["/bin/sh", "/app/entrypoint.sh"]
|