dow-prod-tracker/Dockerfile
DJP df7ddbfb0d Dockerfile: install tsx globally so it stops disappearing
`npm install --no-save tsx` in the runner stage was reporting success
but the tsx binary kept vanishing from node_modules/.bin after the
Next.js standalone node_modules reshuffled its tree. The seed then
failed with "sh: tsx: not found" even after a clean rebuild.

Install tsx globally instead (npm install -g tsx@4 → /usr/local/bin/tsx),
which is PATH-resolvable regardless of any local node_modules churn.

Other seed-adjacent deps stay local (prisma CLI, dotenv, adapter-pg,
bcryptjs) because the seed script explicitly imports them and wants
them resolved from the app's local node_modules.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-20 21:33:55 -04:00

79 lines
2.5 KiB
Docker

FROM node:22-alpine AS base
# FFmpeg for video transcoding (HLS), thumbnail extraction, and metadata
RUN apk add --no-cache ffmpeg
# Install dependencies only when needed
FROM base AS deps
WORKDIR /app
COPY package.json package-lock.json* ./
RUN npm ci --ignore-scripts
RUN npx prisma generate || true
# Rebuild source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Generate Prisma client
RUN npx prisma generate
# Build the Next.js app
RUN npm run build
# Production image
FROM base AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/public ./public
# Copy Next.js standalone output
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
# Copy Prisma schema, config, and migrations for runtime
COPY --from=builder /app/prisma ./prisma
COPY --from=builder /app/prisma.config.ts ./prisma.config.ts
COPY --from=builder /app/src/generated ./src/generated
# Install runtime deps needed for migrations + seed. The Next.js standalone
# bundle covers the app itself, but the seed script (prisma/seed-dow.ts) is
# a separate .ts file executed via tsx.
#
# tsx is installed GLOBALLY (lives at /usr/local/bin/tsx, on PATH
# unconditionally). Installing it locally via `npm install --no-save tsx`
# doesn't work reliably here because Next.js's standalone node_modules
# layout aggressively reshuffles its own tree — the tsx binary was
# repeatedly going missing from node_modules/.bin after what npm reported
# as a successful install. Global install sidesteps that entirely.
#
# Other deps are local (alongside the standalone tree):
# prisma — the migrate-deploy CLI
# dotenv — prisma.config.ts imports dotenv/config
# @prisma/adapter-pg — the driver the seed instantiates directly
# bcryptjs — the seed hashes the admin's temp password
RUN npm install -g tsx@4 && \
npm install --no-save \
prisma@7.4.2 \
dotenv@17.3.1 \
@prisma/adapter-pg@7.4.2 \
bcryptjs@3
# Create uploads directories for media storage
RUN mkdir -p /data/uploads && chown nextjs:nodejs /data/uploads
RUN mkdir -p /app/public/uploads/revisions && chown -R nextjs:nodejs /app/public/uploads
USER nextjs
EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"
CMD ["sh", "-c", "./node_modules/.bin/prisma migrate deploy && node server.js"]