chore(docker): refactor Docker configuration for environment variables and database initialization

- Updated docker-compose.yml to use environment variable fallbacks for configuration.
- Modified Dockerfile to streamline database initialization using Prisma migrations directly.
- Removed init-db.js script as its functionality is now integrated into the Docker CMD.
This commit is contained in:
Julien Froidefond
2025-10-31 12:00:01 +01:00
parent 48e649cf75
commit 5d1239c4de
3 changed files with 38 additions and 93 deletions

View File

@@ -28,12 +28,9 @@ COPY . .
# Set a dummy DATABASE_URL for build time (Prisma needs it to generate client)
ENV DATABASE_URL="file:/tmp/build.db"
# Generate Prisma client
# Generate Prisma client (no DB needed at build time)
RUN pnpm prisma generate
# Initialize the database schema for build time
RUN pnpm prisma migrate deploy || pnpm prisma db push
# Build the application
RUN pnpm run build
@@ -65,12 +62,9 @@ RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy Prisma schema
# Copy Prisma schema and migrations
COPY --from=builder /app/prisma ./prisma
# Copy scripts (including init-db.js)
COPY --from=builder /app/scripts ./scripts
# Copy pnpm node_modules (includes .pnpm store with Prisma client)
COPY --from=builder /app/node_modules ./node_modules
@@ -86,5 +80,7 @@ USER nextjs
EXPOSE 3000
# Start the application with smart database initialization
CMD ["sh", "-c", "node scripts/init-db.js && node server.js"]
# Start the application with Prisma migrations
# For fresh DBs: use db push to apply schema, then mark migrations as applied
# For existing DBs: use migrate deploy to apply incremental migrations
CMD ["sh", "-c", "set +e; if ! pnpm prisma migrate deploy; then echo 'Migration failed, using db push for fresh database...'; pnpm prisma db push --accept-data-loss --skip-generate; for migration in prisma/migrations/*/; do if [ -d \"$migration\" ] && [ -f \"$migration/migration.sql\" ]; then migration_name=$(basename \"$migration\"); pnpm prisma migrate resolve --applied \"$migration_name\" 2>/dev/null || true; fi; done; fi; set -e; exec node server.js"]