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 . .
# 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 npm run build
@@ -26,46 +21,47 @@ FROM node:20-alpine AS runner
WORKDIR /app
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 adduser --system --uid 1001 nextjs
# Copy public folder
COPY --from=builder /app/public ./public
# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next
# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
# Copy standalone build
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
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/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
RUN npm install -g prisma
# Fix permissions for prisma folder
RUN chown -R nextjs:nodejs ./prisma ./node_modules/.prisma ./node_modules/@prisma ./node_modules/prisma
# Create entrypoint script
RUN echo '#!/bin/sh' > /app/entrypoint.sh && \
echo 'echo "Running database migrations..."' >> /app/entrypoint.sh && \
echo 'npx prisma migrate deploy' >> /app/entrypoint.sh && \
echo 'echo "Starting application..."' >> /app/entrypoint.sh && \
echo 'exec node server.js' >> /app/entrypoint.sh && \
chmod +x /app/entrypoint.sh
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
EXPOSE 3000
ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
# Run migrations then start server
CMD ["/bin/sh", "/app/entrypoint.sh"]