fix: install prisma CLI globally for migrations

This commit is contained in:
Erik Silva
2026-01-20 20:41:47 -03:00
parent b3172b14e3
commit 131a847c01

View File

@@ -3,7 +3,6 @@ FROM node:20-alpine AS deps
RUN apk add --no-cache libc6-compat RUN apk add --no-cache libc6-compat
WORKDIR /app WORKDIR /app
# Install dependencies based on the preferred package manager
COPY package.json package-lock.json* ./ COPY package.json package-lock.json* ./
RUN npm ci RUN npm ci
@@ -16,19 +15,20 @@ COPY . .
RUN npx prisma generate RUN npx prisma generate
RUN npm run build RUN npm run build
# Stage 3: Production image, copy all the files and run next # Stage 3: Production image
FROM node:20-alpine AS runner FROM node:20-alpine AS runner
WORKDIR /app WORKDIR /app
ENV NODE_ENV production ENV NODE_ENV=production
# Install prisma CLI globally for migrations
RUN npm install -g prisma
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 public folder
COPY --from=builder /app/public ./public COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next RUN mkdir .next
RUN chown nextjs:nodejs .next RUN chown nextjs:nodejs .next
@@ -36,32 +36,19 @@ RUN chown nextjs:nodejs .next
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 Prisma files for migrations (as root before switching user) # Copy Prisma schema and migrations
COPY --from=builder /app/prisma ./prisma COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
COPY --from=builder /app/node_modules/.prisma ./node_modules/.prisma
COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma
COPY --from=builder /app/node_modules/prisma ./node_modules/prisma
# Fix permissions for prisma folder # Copy prisma client
RUN chown -R nextjs:nodejs ./prisma ./node_modules/.prisma ./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/client ./node_modules/@prisma/client
# Create entrypoint script
COPY <<EOF /app/entrypoint.sh
#!/bin/sh
set -e
echo "Running database migrations..."
./node_modules/.bin/prisma migrate deploy
echo "Migrations completed. Starting application..."
exec node server.js
EOF
RUN chmod +x /app/entrypoint.sh
USER nextjs USER nextjs
EXPOSE 3000 EXPOSE 3000
ENV PORT 3000 ENV PORT=3000
ENV HOSTNAME "0.0.0.0" ENV HOSTNAME="0.0.0.0"
CMD ["/bin/sh", "/app/entrypoint.sh"] # Entrypoint: run migrations then start
CMD sh -c "echo 'Running migrations...' && prisma migrate deploy && echo 'Starting server...' && node server.js"