65 lines
1.6 KiB
Docker
65 lines
1.6 KiB
Docker
# Multi-stage Dockerfile for Next.js application
|
|
|
|
# Build stage
|
|
FROM node:18-alpine AS builder
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package files
|
|
COPY package.json package-lock.json ./
|
|
|
|
# Install all dependencies (including dev dependencies) for build
|
|
RUN npm ci && npm cache clean --force
|
|
|
|
# Copy prisma schema and generate prisma client
|
|
COPY prisma ./prisma
|
|
RUN npx prisma generate
|
|
|
|
# Copy all files
|
|
COPY . .
|
|
|
|
# Set a dummy database URL for build time (will be overridden at runtime)
|
|
ENV DATABASE_URL=file:./dev.db
|
|
|
|
# Build the application
|
|
RUN npm run build
|
|
|
|
# Production stage
|
|
FROM node:18-alpine AS runner
|
|
|
|
# Create a non-root user
|
|
RUN addgroup -g 1001 -S nodejs
|
|
RUN adduser -S nextjs -u 1001 -G nodejs
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package files for production
|
|
COPY package.json package-lock.json ./
|
|
|
|
# Install production dependencies only, skipping prepare script
|
|
RUN npm ci --only=production --ignore-scripts && npm cache clean --force
|
|
|
|
# Copy prisma schema and generated client
|
|
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
|
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
|
|
|
|
# Copy built files from builder stage
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
COPY --from=builder --chown=nextjs:nodejs /app/next.config.js ./next.config.js
|
|
|
|
# Set environment variables
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV DATABASE_URL=file:./dev.db
|
|
|
|
# Expose port
|
|
EXPOSE 3000
|
|
|
|
# Set user
|
|
USER nextjs
|
|
|
|
# Start the application
|
|
CMD ["npm", "start"] |