Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save andresruizdev/a57507557f29942af5f70c98443d209f to your computer and use it in GitHub Desktop.
Save andresruizdev/a57507557f29942af5f70c98443d209f to your computer and use it in GitHub Desktop.

Revisions

  1. andresruizdev created this gist Jan 7, 2025.
    76 changes: 76 additions & 0 deletions Dockerfile - NextJS 15 with Prisma and BetterAuth
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,76 @@
    FROM node:20-alpine AS base

    # Etapa de dependencias
    FROM base AS deps

    RUN apk add --no-cache libc6-compat openssl
    WORKDIR /app

    # Instalar dependencias basadas en el gestor de paquetes
    COPY package.json yarn.lock* ./
    RUN yarn --frozen-lockfile --production=false

    # Etapa de construcción
    FROM base AS builder

    WORKDIR /app
    COPY --from=deps /app/node_modules ./node_modules
    COPY . .

    # Definir variables de entorno usando ARG
    ARG NEXT_PUBLIC_URL
    ARG DATABASE_URL
    ARG BETTER_AUTH_SECRET

    # Usa Building si necesitas controlar variables o lógicas diferentes en construcción y en ejecución

    ENV NEXT_PUBLIC_URL=${NEXT_PUBLIC_URL} \
    DATABASE_URL=${DATABASE_URL} \
    BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET} \
    BUILDING="true"

    # Generar Prisma y construir el proyecto
    RUN yarn prisma generate
    RUN yarn run build && \
    cp -r public .next/standalone/ && \
    cp -r .next/static .next/standalone/.next/ && \
    cp -r prisma .next/standalone/

    # Etapa de ejecución
    FROM base AS runner
    WORKDIR /app

    RUN apk add --no-cache openssl
    # Acá usar las mismas varibles de antes
    ARG NEXT_PUBLIC_URL
    ARG DATABASE_URL
    ARG BETTER_AUTH_SECRET

    # Variables de entorno para producción, deshabilitar telemetría y configurar puerto
    ENV NODE_ENV=production \
    NEXT_TELEMETRY_DISABLED=1 \
    PORT=3000 \
    NEXT_PUBLIC_URL=${NEXT_PUBLIC_URL} \
    DATABASE_URL=${DATABASE_URL} \
    BETTER_AUTH_SECRET=${BETTER_AUTH_SECRET} \
    BUILDING="false" \
    HOSTNAME="0.0.0.0"

    # Crear usuario y grupo para la aplicación
    RUN addgroup --system --gid 1001 nodejs
    RUN adduser --system --uid 1001 nextjs

    #Habilitar esto solo si se necesita compartir volumen con otra instancia
    #RUN mkdir -p /shared
    # Brindar permisos a la carpeta compartida
    #RUN chmod 777 /shared

    # Copiar archivos necesarios para la ejecución
    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/prisma ./prisma/

    USER nextjs

    # Comando para ejecutar la aplicación
    CMD ["sh", "-c", "npx prisma migrate deploy && node server.js"]