89 lines
2.5 KiB
Docker
89 lines
2.5 KiB
Docker
# Use Node.js 18 Alpine as base image
|
|
FROM node:18-alpine AS base
|
|
|
|
# Install dependencies only when needed
|
|
FROM base AS deps
|
|
RUN apk add --no-cache libc6-compat
|
|
WORKDIR /app
|
|
|
|
# Copy package files
|
|
COPY package*.json ./
|
|
COPY prisma ./prisma/
|
|
|
|
# Install ALL dependencies (including devDependencies for build)
|
|
RUN npm install --legacy-peer-deps && npm cache clean --force
|
|
|
|
# Rebuild the source code only when needed
|
|
FROM base AS builder
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
# Set build environment variables
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
ENV SKIP_ENV_VALIDATION=1
|
|
|
|
# Generate Prisma client
|
|
RUN npx prisma generate
|
|
|
|
# Build the application with proper environment
|
|
RUN npm run build
|
|
|
|
# Production image, copy all the files and run next
|
|
FROM base AS runner
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV=production
|
|
ENV NEXT_TELEMETRY_DISABLED=1
|
|
|
|
# Install curl for health checks
|
|
RUN apk add --no-cache curl
|
|
|
|
# Create nextjs user
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nextjs
|
|
|
|
# Copy package files for production install
|
|
COPY --from=builder /app/package*.json ./
|
|
COPY --from=builder /app/prisma ./prisma
|
|
|
|
# Install only production dependencies AND prisma CLI for migrations with legacy peer deps
|
|
RUN npm ci --omit=dev --legacy-peer-deps && npm install prisma sharp --legacy-peer-deps && npm cache clean --force
|
|
|
|
# Copy necessary files
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# Copy built application
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
|
|
|
# Copy generated Prisma client from builder
|
|
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
|
|
|
|
# Create startup script
|
|
RUN echo '#!/bin/sh' > /app/start.sh && \
|
|
echo 'echo "Running database migrations..."' >> /app/start.sh && \
|
|
echo 'npx prisma migrate deploy || echo "Migration failed, but continuing..."' >> /app/start.sh && \
|
|
echo 'echo "Starting application..."' >> /app/start.sh && \
|
|
echo 'exec node server.js' >> /app/start.sh && \
|
|
chmod +x /app/start.sh
|
|
|
|
# Create uploads directory for file uploads
|
|
RUN mkdir -p /app/uploads && chown -R nextjs:nodejs /app/uploads
|
|
|
|
# Change ownership of startup script
|
|
RUN chown nextjs:nodejs /app/start.sh
|
|
|
|
USER nextjs
|
|
|
|
EXPOSE 3000
|
|
|
|
ENV PORT=3000
|
|
ENV HOSTNAME="0.0.0.0"
|
|
|
|
# Health check using curl
|
|
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
|
CMD curl -f http://localhost:3000/api/health || exit 1
|
|
|
|
CMD ["/app/start.sh"] |