From f2d3f34bbfdd363733d0ccb5049fd1913a570623 Mon Sep 17 00:00:00 2001 From: Marcio Bevervanso Date: Tue, 16 Jun 2026 12:27:27 -0300 Subject: [PATCH] =?UTF-8?q?build:=20Dockerfile=20multi-stage=20+=20Next=20?= =?UTF-8?q?standalone=20(deploy=20mais=20r=C3=A1pido)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Substitui o build via Nixpacks (lento, sem cache de deps) por um Dockerfile multi-stage com output: 'standalone'. O stage de deps fica em cache e só reinstala quando package*.json muda; imagem final enxuta roda node server.js. Trocar o build pack do Coolify para Dockerfile. Co-Authored-By: Claude Opus 4.8 --- .dockerignore | 13 +++++++++++++ Dockerfile | 47 +++++++++++++++++++++++++++++++++++++++++++++++ next.config.ts | 4 +++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..ed6fb30 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,13 @@ +node_modules +.next +.git +.gitignore +npm-debug.log* +Dockerfile +.dockerignore +README.md +.env* +settings.json +.vscode +.idea +*.log diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..67221cb --- /dev/null +++ b/Dockerfile @@ -0,0 +1,47 @@ +# syntax=docker/dockerfile:1 + +# ============================================================ +# Multi-stage build para Next.js 16 (output: standalone). +# Bem mais rápido que Nixpacks: o stage de deps fica em cache e +# só reinstala quando package*.json muda. +# ============================================================ + +# ---- deps: instala dependências (camada cacheável) ---- +FROM node:22-alpine AS deps +RUN apk add --no-cache libc6-compat +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci + +# ---- builder: faz o next build ---- +FROM node:22-alpine AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . +ENV NEXT_TELEMETRY_DISABLED=1 +RUN npm run build + +# ---- runner: imagem final enxuta ---- +FROM node:22-alpine AS runner +WORKDIR /app +ENV NODE_ENV=production +ENV NEXT_TELEMETRY_DISABLED=1 +ENV PORT=3000 +ENV HOSTNAME=0.0.0.0 + +RUN addgroup --system --gid 1001 nodejs \ + && adduser --system --uid 1001 nextjs \ + # /app precisa ser gravável pelo usuário nextjs: o app salva settings.json + # no diretório de trabalho em runtime. + && chown nextjs:nodejs /app + +# public/ e .next/static não vão no standalone por padrão — copiamos à mão. +COPY --from=builder --chown=nextjs:nodejs /app/public ./public +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static + +USER nextjs +EXPOSE 3000 + +# server.js é gerado pelo output standalone do Next. +CMD ["node", "server.js"] diff --git a/next.config.ts b/next.config.ts index e9ffa30..a4adbe8 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,7 +1,9 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { - /* config options here */ + // Gera .next/standalone com só o necessário — imagem Docker bem menor e + // deploy mais rápido (não precisa instalar node_modules no runtime). + output: 'standalone', }; export default nextConfig;