Fix Docker Prisma constructor error
- Remove --ignore-scripts from npm ci in builder stage - Simplify Prisma client initialization - Ensure proper file copying order in Docker - Remove async connection handling that may cause issues
This commit is contained in:
11
Dockerfile
11
Dockerfile
@@ -18,7 +18,7 @@ WORKDIR /app
|
|||||||
|
|
||||||
# Copy package files and install all dependencies (including dev)
|
# Copy package files and install all dependencies (including dev)
|
||||||
COPY package.json package-lock.json* ./
|
COPY package.json package-lock.json* ./
|
||||||
RUN npm ci --ignore-scripts
|
RUN npm ci
|
||||||
|
|
||||||
# Copy source code
|
# Copy source code
|
||||||
COPY . .
|
COPY . .
|
||||||
@@ -29,6 +29,7 @@ RUN npx prisma generate
|
|||||||
# Build Next.js application
|
# Build Next.js application
|
||||||
ENV NEXT_TELEMETRY_DISABLED=1
|
ENV NEXT_TELEMETRY_DISABLED=1
|
||||||
ENV AUTH_TRUST_HOST=true
|
ENV AUTH_TRUST_HOST=true
|
||||||
|
ENV NODE_ENV=production
|
||||||
RUN npm run build
|
RUN npm run build
|
||||||
|
|
||||||
# Production image, copy all the files and run next
|
# Production image, copy all the files and run next
|
||||||
@@ -42,13 +43,15 @@ ENV NEXT_TELEMETRY_DISABLED=1
|
|||||||
RUN addgroup --system --gid 1001 nodejs
|
RUN addgroup --system --gid 1001 nodejs
|
||||||
RUN adduser --system --uid 1001 nextjs
|
RUN adduser --system --uid 1001 nextjs
|
||||||
|
|
||||||
# Copy necessary files
|
# Copy the standalone output
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
|
||||||
|
|
||||||
|
# Copy Prisma files
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/@prisma ./node_modules/@prisma
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
|
|
||||||
|
|
||||||
USER nextjs
|
USER nextjs
|
||||||
|
|
||||||
|
|||||||
14
lib/db.ts
14
lib/db.ts
@@ -4,22 +4,8 @@ const globalForPrisma = globalThis as unknown as {
|
|||||||
prisma: PrismaClient | undefined
|
prisma: PrismaClient | undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Prisma client with connection error handling
|
|
||||||
export const prisma = globalForPrisma.prisma ?? new PrismaClient({
|
export const prisma = globalForPrisma.prisma ?? new PrismaClient({
|
||||||
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
|
log: process.env.NODE_ENV === 'development' ? ['error', 'warn'] : ['error'],
|
||||||
})
|
})
|
||||||
|
|
||||||
// Handle connection errors
|
|
||||||
prisma.$connect().catch((error) => {
|
|
||||||
console.error('Failed to connect to database:', error)
|
|
||||||
// Don't throw here - let individual queries handle connection errors
|
|
||||||
})
|
|
||||||
|
|
||||||
// Graceful shutdown
|
|
||||||
if (typeof window === 'undefined') {
|
|
||||||
process.on('beforeExit', async () => {
|
|
||||||
await prisma.$disconnect()
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
|
if (process.env.NODE_ENV !== 'production') globalForPrisma.prisma = prisma
|
||||||
Reference in New Issue
Block a user