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 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
node_modules
|
||||
.next
|
||||
.git
|
||||
.env
|
||||
.env.local
|
||||
!.env.local.example
|
||||
npm-debug.log*
|
||||
uploads
|
||||
+34
@@ -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"]
|
||||
+2
-1
@@ -1,7 +1,8 @@
|
||||
import type { NextConfig } from "next";
|
||||
|
||||
const nextConfig: NextConfig = {
|
||||
/* config options here */
|
||||
// برای build خروجی مستقل (بدون نیاز به node_modules کامل) — لازم برای ایمیج Docker سبک
|
||||
output: "standalone",
|
||||
};
|
||||
|
||||
export default nextConfig;
|
||||
|
||||
Reference in New Issue
Block a user