fix: proper prisma migration setup in Dockerfile

This commit is contained in:
Erik Silva
2026-01-20 19:13:34 -03:00
parent 4e2c4632cf
commit b3172b14e3

View File

@@ -13,11 +13,6 @@ WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules COPY --from=deps /app/node_modules ./node_modules
COPY . . COPY . .
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1
RUN npx prisma generate RUN npx prisma generate
RUN npm run build RUN npm run build
@@ -26,46 +21,47 @@ FROM node:20-alpine AS runner
WORKDIR /app WORKDIR /app
ENV NODE_ENV production ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# 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 public folder
COPY --from=builder /app/public ./public COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache # Set the correct permission for prerender cache
RUN mkdir .next RUN mkdir .next
RUN chown nextjs:nodejs .next RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size # Copy standalone build
# https://nextjs.org/docs/advanced-features/output-file-tracing
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 # Copy Prisma files for migrations (as root before switching user)
COPY --from=builder /app/prisma ./prisma COPY --from=builder /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 COPY --from=builder /app/node_modules/@prisma ./node_modules/@prisma
COPY --from=builder /app/node_modules/prisma ./node_modules/prisma
# Install prisma CLI for migrations # Fix permissions for prisma folder
RUN npm install -g prisma RUN chown -R nextjs:nodejs ./prisma ./node_modules/.prisma ./node_modules/@prisma ./node_modules/prisma
# Create entrypoint script # Create entrypoint script
RUN echo '#!/bin/sh' > /app/entrypoint.sh && \ COPY <<EOF /app/entrypoint.sh
echo 'echo "Running database migrations..."' >> /app/entrypoint.sh && \ #!/bin/sh
echo 'npx prisma migrate deploy' >> /app/entrypoint.sh && \ set -e
echo 'echo "Starting application..."' >> /app/entrypoint.sh && \ echo "Running database migrations..."
echo 'exec node server.js' >> /app/entrypoint.sh && \ ./node_modules/.bin/prisma migrate deploy
chmod +x /app/entrypoint.sh 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
# set hostname to localhost
ENV HOSTNAME "0.0.0.0" ENV HOSTNAME "0.0.0.0"
# Run migrations then start server
CMD ["/bin/sh", "/app/entrypoint.sh"] CMD ["/bin/sh", "/app/entrypoint.sh"]