DockerAdvanced
Next.js অ্যাপ Docker-এ
Next.js standalone output দিয়ে ছোট ও দ্রুত production image — multi-stage build — বাংলায়।
11টি অংশবাংলা ভাষায়
Next.js-কে Docker-এ চালাতে `output: "standalone"` ব্যবহার করা ভালো — এটি সব প্রয়োজনীয় runtime file একটি ছোট folder-এ রাখে; ফলে image অনেক হালকা হয়।
next.config.ts-এ standalone
next.configts
ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;Multi-stage Dockerfile
Dockerfiledockerfile
dockerfile
# Stage 1: build
FROM node:20-alpine AS builder
WORKDIR /app
COPY package*.json ./
RUN npm ci
COPY . .
RUN npm run build
# Stage 2: runtime
FROM node:20-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
# standalone output file-গুলোই প্রয়োজন
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
COPY --from=builder /app/public ./public
EXPOSE 3000
USER node
CMD ["node", "server.js"]Build ও verify
Run stepsbash
bash
docker build -t my-next-app .
docker run -d -p 3000:3000 \
-e DATABASE_URL=mongodb://... \
--name next-app \
my-next-app
# homepage check
curl -I http://localhost:3000with Docker-এর বিশেষ ভুল
.nextওnode_modules-কে.dockerignore-এ রাখুনoutput: standaloneছাড়া সব file কপি করলে image ভারী হয়- প্রতি পরিবর্তনে cache ভাঙা আটকাতে
COPY package*.jsonআলাদা ধাপে দিন - Dev mode-এ live reload চাইলে bind mount +
npm run devব্যবহার করুন
.dockerignoretext
text
node_modules
.next
.git
.env
.env.*
Dockerfile
.dockerignoreTrailing advice
Static export (SSG-only) হলে image-এ এই ছোট force standalone দরকার নয় — next export output-কে nginx-এর মতো web server-এ serve করুন। ...না হলে সেটাও সম্ভব।
শেষ প্যারায় নিজে practice করুন — কোড কপি করে নিজের এডিটরে চালিয়ে দেখুন।