# syntax=docker/dockerfile:1
# Songs2VID OSS self-hosted image.

FROM node:22-bookworm-slim AS deps
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends openssl ca-certificates \
  && rm -rf /var/lib/apt/lists/*
COPY package.json package-lock.json ./
COPY prisma ./prisma
RUN npm install

FROM node:22-bookworm-slim AS builder
WORKDIR /app
RUN apt-get update && apt-get install -y --no-install-recommends openssl ca-certificates \
  && rm -rf /var/lib/apt/lists/*
COPY --from=deps /app/node_modules ./node_modules
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN npx prisma generate && npm run build

FROM node:22-bookworm-slim AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV NEXT_TELEMETRY_DISABLED=1
ENV UPLOAD_DIR=/app/uploads
ENV FFMPEG_PATH=ffmpeg
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
ENV HOME=/home/nextjs
ENV npm_config_cache=/tmp/npm-cache

RUN apt-get update && apt-get install -y --no-install-recommends \
    openssl ca-certificates ffmpeg \
  && rm -rf /var/lib/apt/lists/* \
  && groupadd --system --gid 1001 nodejs \
  && useradd --system --uid 1001 --gid nodejs --create-home --home-dir /home/nextjs nextjs \
  && mkdir -p /app/uploads \
  && chown nextjs:nodejs /app /app/uploads

USER nextjs

COPY --chown=nextjs:nodejs package.json package-lock.json ./
COPY --chown=nextjs:nodejs prisma ./prisma
RUN npm install --omit=dev \
  && npx prisma generate \
  && rm -rf /tmp/npm-cache

COPY --chown=nextjs:nodejs --from=builder /app/.next/standalone/server.js ./server.js
COPY --chown=nextjs:nodejs --from=builder /app/.next/standalone/.next ./.next
COPY --chown=nextjs:nodejs --from=builder /app/.next/static ./.next/static
COPY --chown=nextjs:nodejs --from=builder /app/public ./public
COPY --chown=nextjs:nodejs --from=builder /app/assets ./assets
COPY --chown=nextjs:nodejs --from=builder /app/worker ./worker
COPY --chown=nextjs:nodejs --from=builder /app/lib ./lib
COPY --chown=nextjs:nodejs --from=builder /app/tsconfig.json ./tsconfig.json

EXPOSE 3000
CMD ["node", "server.js"]
