From 501fa294f409df03c6cc620c034ed3a6ed940b41 Mon Sep 17 00:00:00 2001 From: mobiniqow Date: Wed, 16 Sep 2026 00:01:26 +0330 Subject: [PATCH] Add Docker support (standalone Next.js output) Multi-stage Dockerfile producing a minimal runtime image via next.config.ts's output:"standalone". NEXT_PUBLIC_API_URL is taken as a build arg since it gets baked into the client bundle at build time. Co-Authored-By: Claude Sonnet 5 --- .dockerignore | 8 ++++++++ Dockerfile | 34 ++++++++++++++++++++++++++++++++++ next.config.ts | 3 ++- 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 .dockerignore create mode 100644 Dockerfile diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..9f4f90b --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +node_modules +.next +.git +.env +.env.local +!.env.local.example +npm-debug.log* +uploads diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..20cb2d2 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,34 @@ +# ---- وابستگی‌ها ---- +FROM node:22-alpine AS deps +WORKDIR /app +COPY package.json package-lock.json ./ +RUN npm ci + +# ---- build ---- +FROM node:22-alpine AS builder +WORKDIR /app +COPY --from=deps /app/node_modules ./node_modules +COPY . . + +# NEXT_PUBLIC_* موقع build توی باندل کلاینت جاسازی می‌شه، پس باید همینجا +# به‌عنوان build arg پاس داده بشه (نه فقط env زمان اجرا) +ARG NEXT_PUBLIC_API_URL=http://localhost:8080/api/v1 +ENV NEXT_PUBLIC_API_URL=$NEXT_PUBLIC_API_URL + +RUN npm run build + +# ---- اجرا (ایمیج نهایی سبک، فقط خروجی standalone) ---- +FROM node:22-alpine AS runner +WORKDIR /app +ENV NODE_ENV=production + +RUN addgroup -S nodejs && adduser -S nextjs -G nodejs + +COPY --from=builder /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 +ENV PORT=3000 +CMD ["node", "server.js"] diff --git a/next.config.ts b/next.config.ts index e9ffa30..f99279a 100644 --- a/next.config.ts +++ b/next.config.ts @@ -1,7 +1,8 @@ import type { NextConfig } from "next"; const nextConfig: NextConfig = { - /* config options here */ + // برای build خروجی مستقل (بدون نیاز به node_modules کامل) — لازم برای ایمیج Docker سبک + output: "standalone", }; export default nextConfig;