53 lines
1.0 KiB
Docker
53 lines
1.0 KiB
Docker
# Multi-stage Dockerfile for Next.js Application
|
|
|
|
# Build stage
|
|
FROM node:24-alpine AS builder
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package files
|
|
COPY package*.json ./
|
|
|
|
# Install dependencies
|
|
RUN npm ci --only=production
|
|
|
|
# Copy source code
|
|
COPY . .
|
|
|
|
# Build the Next.js application
|
|
RUN npm run build
|
|
|
|
# Production stage
|
|
FROM node:24-alpine AS production
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Create non-root user
|
|
RUN addgroup -g 1001 -S nodejs && \
|
|
adduser -S nextjs -u 1001
|
|
|
|
# Copy dependencies from builder stage
|
|
COPY --from=builder /app/node_modules ./node_modules
|
|
COPY --from=builder /app/.next ./.next
|
|
COPY --from=builder /app/package.json ./package.json
|
|
|
|
# Copy source code
|
|
COPY --from=builder /app/.env ./.env
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# Change ownership
|
|
RUN chown -R nextjs:nodejs /app
|
|
USER nextjs
|
|
|
|
# Expose port
|
|
EXPOSE 3000
|
|
|
|
# Health check
|
|
HEALTHCHECK --interval=30s --timeout=30s --start-period=5s --retries=3 \
|
|
CMD curl -f http://localhost:3000/health || exit 1
|
|
|
|
# Start command
|
|
CMD ["npm", "start"]
|