56 lines
1.3 KiB
Docker
56 lines
1.3 KiB
Docker
# Stage 1: Build stage
|
|
FROM node:18-alpine AS builder
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Copy package.json and package-lock.json
|
|
COPY package*.json ./
|
|
|
|
# Install dependencies
|
|
RUN npm ci
|
|
|
|
# Copy all files
|
|
COPY . .
|
|
|
|
# Build the application
|
|
RUN npm run build
|
|
|
|
# Stage 2: Production stage
|
|
FROM node:18-alpine AS runner
|
|
|
|
# Set working directory
|
|
WORKDIR /app
|
|
|
|
# Set environment variables
|
|
ENV NODE_ENV=production
|
|
|
|
# Create app directory
|
|
RUN mkdir -p /app
|
|
|
|
# Don't run production as root
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nextjs
|
|
RUN chown -R nextjs:nodejs /app
|
|
|
|
# Copy necessary files from builder stage
|
|
COPY --from=builder --chown=nextjs:nodejs /app/package*.json ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/next.config.ts ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next ./.next
|
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
|
|
|
|
# Create data directory and ensure it's writable
|
|
RUN mkdir -p /app/data
|
|
COPY --from=builder --chown=nextjs:nodejs /app/data ./data
|
|
RUN chown -R nextjs:nodejs /app/data
|
|
|
|
# Switch to non-root user
|
|
USER nextjs
|
|
|
|
# Expose the port the app will run on
|
|
EXPOSE 3000
|
|
|
|
# Command to run the application
|
|
CMD ["npm", "start"]
|