54 lines
1.2 KiB
Docker
54 lines
1.2 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 dependencies
|
|
RUN npm ci --only=production && npm cache clean --force
|
|
|
|
# Copy all files
|
|
COPY . .
|
|
|
|
# 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
|
|
RUN npm ci --only=production && npm cache clean --force
|
|
|
|
# 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
|
|
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
|
|
|
|
# Set environment variables
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
|
|
# Expose port
|
|
EXPOSE 3000
|
|
|
|
# Set user
|
|
USER nextjs
|
|
|
|
# Start the application
|
|
CMD ["npm", "start"] |