Thuta Learning
ရှာဖွေရန်
AdvancedWeb Developmentintermediate

Docker ဖြင့် Self-hosting

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

ဒီခန်းပြီးရင် ဘာတတ်သွားမလဲ

  • Standalone output ကိုဖွင့်ရန်
  • Production Docker image တည်ဆောက်ရန်
  • Reverse proxy နှင့် persistent services ကို စဉ်းစားရန်

Vercel မသုံးဘဲ ကိုယ့် server၊ Kubernetes သို့မဟုတ် cloud container service ပေါ်တင်ချင်တာလည်း ဖြစ်နိုင်ပါတယ်။ Self-hosting က လွတ်လပ်မှုပိုပေးပေမယ့် reverse proxy၊ process restart၊ cache coordination နဲ့ security patch တို့ကို ကိုယ်တိုင်တာဝန်ယူရပါတယ်။

နားလည်ထားရမယ့် အချက်

next.config.ts မှ output: standalone ဖွင့်ရင် runtime အတွက်လိုအပ်တဲ့ file များကို သီးခြားထုတ်ပေးပါတယ်။ Multi-stage Docker build က dependency install၊ build နဲ့ runtime ကိုခွဲပြီး final image သေးစေပါတယ်။ Production မှာ Next.js server ကို internet ဆီတိုက်ရိုက်မဖွင့်ဘဲ nginx သို့မဟုတ် cloud load balancer နောက်ထားဖို့ official guide ကအကြံပြုပါတယ်။ Multiple instance သုံးရင် cache နဲ့ invalidation coordination ကိုလည်း စီစဉ်ရပါတယ်။

အတူတူ စမ်းရေးကြည့်မယ်

dockerfile
# next.config.ts တွင် output: "standalone" ထည့်ပါ
FROM node:22-alpine AS builder
WORKDIR /app
COPY . .
RUN corepack enable && pnpm install --frozen-lockfile
RUN pnpm build

FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
CMD ["node", "server.js"]

Code က ဘယ်လိုအလုပ်လုပ်သလဲ

Builder stage က app ကို build လုပ်ပြီး runner stage က standalone output နဲ့ public assets ပဲယူပါတယ်။ Container က port 3000 မှာ server.js run ပါတယ်။ Database နဲ့ uploads ကို container filesystem ထဲ permanent မသိမ်းပါနဲ့။

You should see
Standalone Next.js server ပါသော production Docker image တည်ဆောက်နိုင်မည်။

၅ မိနစ် စမ်းကြည့်

Docker health check၊ non-root user နဲ့ reverse proxy HTTPS ပါဝင်သော deployment diagram တစ်ခုရေးပါ။

Next.js — Self-HostingNext.js

ဒီနေရာမှာ လူအများမှားတတ်တယ်

  • Next.js server ကို reverse proxy မပါဘဲ internet ဆီတိုက်ရိုက်ဖွင့်ခြင်း
  • Upload file နဲ့ session ကို container local filesystem/memory ထဲ permanent သိမ်းခြင်း

လေ့ကျင့်ခန်း

Docker health check၊ non-root user နဲ့ reverse proxy HTTPS ပါဝင်သော deployment diagram တစ်ခုရေးပါ။

You'll know it worked when: Standalone Next.js server ပါသော production Docker image တည်ဆောက်နိုင်မည်။

Docker ဖြင့် Self-hosting | Thuta Learning