ရိုးရိုးလေး ရှင်းပြမယ်
ရိုးရိုးလေး ရှင်းရရင်
Container image ဆိုတာ ဘူးထဲမှာ ဟင်းချက်ပြီးသားကို ထုပ်ပြီး ဘယ်မီးဖိုမှာမဆို အပူပေးလို့ ရအောင် ပို့တာနဲ့ တူပါတယ်။ ဘူးထဲမှာ ဟင်းနဲ့ အပူပေးဖို့ လိုတာပဲ ပါရပါမယ်၊ ချက်ခဲ့တဲ့ ဒယ်အိုး၊ ဟင်းသီးဟင်းရွက် အခွံတွေ မပါသင့်ပါဘူး။
Multi-stage Docker build က ဒီအတိုင်းပါပဲ: ပထမ stage တွေမှာ dependency install လုပ်ပြီး app ကို build လုပ်ကာ နောက်ဆုံး stage ထဲကို run ဖို့ လိုတဲ့ file တွေကိုပဲ ကူးထည့်ပါတယ်။ Vercel လို platform အစား ကိုယ့် server ပေါ်မှာ run ရင် လွတ်လပ်မှု ပိုရပေမယ့် HTTPS၊ restart၊ security patch တွေကို ကိုယ်တိုင် တာဝန်ယူရပါတယ်။
Problem → Why → Solution
ပြဿနာ: 1 GB image၊ root user၊ ပျောက်သွားတဲ့ upload တွေ
ပထမဆုံး Dockerfile ရေးတဲ့ developer တစ်ယောက်က project တစ်ခုလုံးကို copy ကူး၊ install လုပ်၊ build လုပ်ပြီး next start နဲ့ run ပါတယ်။ Image ထဲမှာ dev dependency အားလုံး၊ source code၊ .git folder နဲ့ မတော်တဆ .env.local file ပါ ပါသွားပြီး root user နဲ့ run နေပါတယ်။
User တွေ တင်တဲ့ ပုံတွေကို container ထဲက uploads folder မှာ သိမ်းထားလို့ version အသစ် deploy လုပ်လိုက်တိုင်း ပုံတွေ အကုန် ပျောက်သွားပါတယ်။ Port 3000 ကိုလည်း internet ကို တိုက်ရိုက် ဖွင့်ထားလို့ HTTPS မရှိပါဘူး။
FROM node:22
WORKDIR /app
COPY . . # also copies .env.local, .git and node_modules
RUN npm install && npm run build
EXPOSE 3000
CMD ["npm", "start"] # runs as root, with every dev dependency insideဘာကြောင့် ဒီ image က ပြဿနာရှိတာလဲ
Image ထဲမှာ ပါတဲ့ file တိုင်းက download လုပ်ရတဲ့ အရွယ်အစားကို တိုးစေပြီး security scan မှာ စစ်ရမယ့် package တွေကို တိုးစေပါတယ်။ Image ကို ရနိုင်သူ ဘယ်သူမဆို .env.local ထဲက secret တွေကို ဖတ်နိုင်ပါတယ်။ Root နဲ့ run ရင် app မှာ အားနည်းချက် တစ်ခုခု ရှိတဲ့အခါ attacker က container ထဲမှာ အခွင့်အရေး အပြည့် ရသွားပါတယ်။ Container ကို အချိန်မရွေး ဖျက်ပြီး အသစ် ပြန်ဆောက်နိုင်တဲ့ အရာအဖြစ် ဆက်ဆံရပါမယ်။
Core concept: standalone output နဲ့ multi-stage build
next.config.ts ထဲမှာ output: "standalone" ထည့်လိုက်ရင် next build က .next/standalone ဆိုတဲ့ folder တစ်ခု ထုတ်ပေးပါတယ်။ Next.js က သင့် code က တကယ် import လုပ်တဲ့ file တွေကို ခြေရာခံပြီး production မှာ လိုတဲ့ node_modules အစိတ်အပိုင်းတွေနဲ့ server.js ဆိုတဲ့ server အသေးလေးကိုပဲ ထည့်ပါတယ်။ ဒါကြောင့် next start သုံးဖို့ next package တစ်ခုလုံး install လုပ်စရာ မလိုဘဲ node server.js နဲ့ တိုက်ရိုက် run နိုင်ပါတယ်။
အရေးကြီးတဲ့ အချက်က standalone folder ထဲမှာ public folder နဲ့ .next/static (CSS၊ client JavaScript chunk တွေ) မပါတာပါ။ ဒါတွေကို CDN ကနေ serve လုပ်ချင်တဲ့ team တွေ ရှိလို့ပါ။ Container တစ်ခုတည်းကနေ serve လုပ်မယ်ဆိုရင် ဒီ folder နှစ်ခုကို standalone ဘေးမှာ ကိုယ်တိုင် ကူးထည့်ရပါမယ်၊ မဟုတ်ရင် page HTML က ပေါ်ပေမယ့် style နဲ့ JavaScript တွေ 404 ဖြစ်ပါတယ်။
| Stage | လုပ်တဲ့ အလုပ် | နောက်ဆုံး image ထဲ ပါလား |
|---|---|---|
| deps | Lockfile အတိုင်း dependency install | မပါ |
| builder | Source ကူးပြီး next build | မပါ |
| runner | standalone + static + public ကိုပဲ ကူးပြီး run | ဒါပဲ ပါ |
Stage ခွဲထားတာရဲ့ နောက်ထပ် အကျိုးက Docker layer cache ပါ။ package.json နဲ့ lockfile ကို အရင်ကူးပြီး install လုပ်ထားလို့ source code ပဲ ပြောင်းရင် install အဆင့်ကို cache ကနေ ပြန်သုံးပြီး build က အများကြီး မြန်လာပါတယ်။
Production မှာ Next.js server ကို internet ကို တိုက်ရိုက် မဖွင့်ဘဲ nginx၊ Caddy ဒါမှမဟုတ် cloud load balancer လို reverse proxy နောက်မှာ ထားဖို့ Next.js ရဲ့ self-hosting guide က အကြံပြုပါတယ်။ Proxy က HTTPS certificate၊ compression၊ request အရွယ်အစား ကန့်သတ်ချက်၊ rate limiting နဲ့ နှေးတဲ့ client တွေကို ကိုင်တွယ်ပေးပြီး Node process က app logic ကိုပဲ အာရုံစိုက်နိုင်ပါတယ်။
ကိုယ်တိုင် host လုပ်ရင် operation အလုပ်တွေလည်း ကိုယ့်ဆီ ရောက်လာပါတယ်။ Container crash ဖြစ်ရင် တစ်ခုခုက ပြန်စပေးရပါမယ် (Docker ရဲ့ restart policy၊ systemd ဒါမှမဟုတ် Kubernetes လို orchestrator)။ Node.js ဒါမှမဟုတ် Alpine က security fix ထုတ်ရင် image ကို တစ်ယောက်ယောက်က ပြန် build လုပ်ရပါမယ်။ Container ပျက်သွားတာနဲ့အတူ သူ့ output ပါ ပျောက်သွားလို့ log တွေကို ဘယ်ကို ပို့မလဲ ဆိုတာကိုလည်း တစ်ယောက်ယောက်က ဆုံးဖြတ်ရပါမယ်။
- deps က lockfile အတိုင်း install
- builder က next build run
- runner က လိုတာပဲ ကူး
- Proxy နောက်က non-root နဲ့ run
Container ကို အချိန်မရွေး ဖျက်ပစ်လို့ ရတဲ့ အရာအဖြစ် ဆက်ဆံပါ။ ထိန်းသိမ်းရမယ့် data တွေကို database နဲ့ object storage မှာပဲ ထားပါ။
အခြေခံ ပုံစံ: multi-stage Dockerfile
FROM <base-image> AS deps
COPY <package manifest and lockfile> ./
RUN <install dependencies from the lockfile>
FROM <base-image> AS builder
COPY --from=deps <installed dependencies> ./
COPY <source> .
RUN <build command>
FROM <base-image> AS runner
COPY --from=builder <only the runtime output> ./
CMD ["<start command>"]FROM တိုင်းက stage အသစ် စတာပါ။ COPY --from=<stage> က အရင် stage ကနေ file တွေကို ရွေးကူးပြီး နောက်ဆုံး FROM ရဲ့ stage ကပဲ image အဖြစ် ကျန်ပါတယ်။
ဥပမာ: Myanmar Notes အတွက် Dockerfile
ဒီ Dockerfile က Next.js ရဲ့ တရားဝင် with-docker ဥပမာရဲ့ ပုံစံကို လိုက်ထားပြီး pnpm ကို သုံးပါတယ်။ အရင်ဆုံး next.config.ts မှာ output: "standalone" ကို ဖွင့်ထားရပါမယ်။ Project root မှာ .dockerignore file ထားပြီး node_modules၊ .next၊ .git နဲ့ .env* file တွေကို build context ထဲ မပါအောင် ဖယ်ထားပါ။ Project မှာ public folder မရှိရင် အဲဒီ COPY လိုင်းက fail ဖြစ်လို့ folder အလွတ် တစ်ခု ဖန်တီးထားပါ။
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;# Requires output: "standalone" in next.config.ts
FROM node:22-alpine AS deps
WORKDIR /app
COPY package.json pnpm-lock.yaml ./
RUN corepack enable pnpm && pnpm install --frozen-lockfile
FROM node:22-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
# Public values are inlined into browser JS here, at build time
ARG NEXT_PUBLIC_SITE_URL
ENV NEXT_PUBLIC_SITE_URL=$NEXT_PUBLIC_SITE_URL
RUN corepack enable pnpm && pnpm build
FROM node:22-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjs
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 HOSTNAME=0.0.0.0
CMD ["node", "server.js"]Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- Install once, cache often
COPY package.json pnpm-lock.yaml ./Source code မကူးခင် manifest နဲ့ lockfile ကိုပဲ အရင်ကူးပါတယ်။ ဒီ file နှစ်ခု မပြောင်းသရွေ့ Docker က install layer ကို cache ကနေ ပြန်သုံးပါတယ်။ --frozen-lockfile က lockfile နဲ့ မကိုက်ရင် fail ဖြစ်စေလို့ build တိုင်း version တူတူ ရပါတယ်။
- Build-time public values
ARG NEXT_PUBLIC_SITE_URLNEXT_PUBLIC_ value တွေက pnpm build အချိန်မှာ JavaScript ထဲ ရေးထည့်ခံရလို့ builder stage မှာ --build-arg နဲ့ ပေးရပါတယ်။ docker run -e နဲ့ ပေးရင် browser code ကို မပြောင်းနိုင်ပါဘူး။ Secret တွေကိုတော့ ARG နဲ့ မပေးပါနဲ့၊ image history ထဲ ကျန်နိုင်လို့ပါ။
- A user without privileges
RUN addgroup --system --gid 1001 nodejs && adduser --system --uid 1001 nextjsSystem user တစ်ယောက် ဖန်တီးပြီး USER nextjs နဲ့ ပြောင်းလိုက်လို့ server က root မဟုတ်ဘဲ run ပါတယ်။ --chown က standalone file တွေကို ဒီ user ပိုင်အောင် လုပ်ပေးပါတယ်။
- The three things to copy
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/staticStandalone output၊ .next/static နဲ့ public ဆိုတဲ့ သုံးခုကို ကူးရပါတယ်။ .next/static ကို မေ့ရင် page ပေါ်ပေမယ့် CSS နဲ့ client JavaScript တွေ 404 ဖြစ်ပြီး button တွေ အလုပ်မလုပ်ပါဘူး။
- Listen on every interface
ENV PORT=3000 HOSTNAME=0.0.0.0server.js က PORT နဲ့ HOSTNAME env ကို ဖတ်ပြီး ဘယ်မှာ listen လုပ်မလဲ ဆုံးဖြတ်ပါတယ်။ Docker က HOSTNAME ကို container ID အဖြစ် အလိုလို သတ်မှတ်တတ်လို့ 0.0.0.0 လို့ ပြန်သတ်မှတ်ထားမှ container ပြင်ပကနေ ချိတ်လို့ ရပါတယ်။
docker build --build-arg NEXT_PUBLIC_SITE_URL=https://notes.example.com -t myanmar-notes . က tag တပ်ထားတဲ့ image နဲ့ ပြီးဆုံးပါတယ်။ docker run -p 3000:3000 --env-file .env.production myanmar-notes က server.js ကို nextjs user နဲ့ စတင်ပြီး http://localhost:3000 မှာ app ကို ဖွင့်ကြည့်နိုင်ပါတယ် (log စာသား အတိအကျက version အလိုက် ကွာနိုင်ပါတယ်)။Env: image ထဲ ထည့်မလား၊ run တဲ့အခါ ပေးမလား
| Value | ပေးရမယ့် နေရာ | အကြောင်းရင်း |
|---|---|---|
| NEXT_PUBLIC_SITE_URL | docker build --build-arg | Build time မှာ browser JS ထဲ ရေးထည့်လို့ |
| DATABASE_URL၊ AUTH_SECRET | docker run --env-file ဒါမှမဟုတ် orchestrator secret | Image ထဲ မပါဘဲ environment အလိုက် ကွဲလို့ |
| APP_VERSION | Build arg ဒါမှမဟုတ် run env | Log နဲ့ health check မှာ release ကို ပြဖို့ |
docker build --build-arg NEXT_PUBLIC_SITE_URL=https://notes.example.com \
-t myanmar-notes:1.4.0 .
docker run -d --name notes -p 127.0.0.1:3000:3000 \
--env-file .env.production -e APP_VERSION=1.4.0 myanmar-notes:1.4.0Port ကို 127.0.0.1:3000 နဲ့ပဲ publish လုပ်ထားလို့ server ပေါ်က reverse proxy ကပဲ ချိတ်နိုင်ပြီး internet ကနေ port 3000 ကို တိုက်ရိုက် မရောက်နိုင်ပါဘူး။ Build time မှာ database ကို လိုတဲ့ static page တွေ ရှိရင် builder stage ကို ဘယ်လို ချိတ်မလဲ ကြိုစီစဉ်ပါ၊ ဒါမှမဟုတ် အဲဒီ page တွေကို dynamic ဖြစ်အောင် ထားပါ။
Health check နဲ့ instance အများကြီး run ခြင်း
Docker၊ Kubernetes နဲ့ load balancer တွေက container ကို ပုံမှန် "အသက်ရှင်လား" မေးကြည့်ပြီး မဖြေရင် restart လုပ်တာ ဒါမှမဟုတ် traffic မပို့တော့တာ လုပ်ပါတယ်။ Alpine image မှာ curl မပါဘဲ BusyBox ရဲ့ wget ပါလို့ HEALTHCHECK မှာ wget ကို သုံးနိုင်ပါတယ်။
HEALTHCHECK --interval=30s --timeout=3s --start-period=10s \
CMD wget -q -O /dev/null http://127.0.0.1:3000/api/health || exit 1Container တစ်ခုထက် ပိုရင်
Instance အများကြီး run ရင် container တစ်ခုချင်းရဲ့ memory နဲ့ disk ထဲက cache ကို တခြား instance တွေ မမြင်ရပါဘူး။ revalidatePath လို invalidation က instance တစ်ခုတည်းကိုပဲ ထိနိုင်လို့ shared cache handler ကို စဉ်းစားရပါမယ်။ Server Action encryption key လို instance အားလုံး တူရမယ့် setting တွေလည်း ရှိပါတယ်။ အသေးစိတ်ကို Next.js self-hosting guide မှာ ကိုယ့် version အတွက် စစ်ပါ။
လက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ
Company တွေက container image ကို CI ထဲမှာ တစ်ကြိမ်ပဲ build လုပ်ပြီး commit SHA ဒါမှမဟုတ် version နဲ့ tag တပ်ကာ registry ထဲ push လုပ်ပါတယ်။ Staging နဲ့ production က image တူတူကို ဆွဲသုံးပြီး secret တွေကိုပဲ environment အလိုက် ပြောင်းပေးပါတယ်။ ဒါကြောင့် staging မှာ စမ်းခဲ့တဲ့ bit တိုင်း production မှာ အတိအကျ run ပါတယ်။
User တွေ upload လုပ်တဲ့ file တွေကို S3 လို object storage မှာ၊ session နဲ့ rate-limit counter တွေကို database ဒါမှမဟုတ် Redis မှာ သိမ်းပါတယ်။ ဒါမှ container ကို အချိန်မရွေး အစားထိုးနိုင်ပြီး instance အရေအတွက်ကို traffic အလိုက် တိုး/လျှော့ နိုင်ပါတယ်။ Base image (node:22-alpine) ကိုလည်း security patch အတွက် ပုံမှန် ပြန် build လုပ်ပြီး image scanner နဲ့ စစ်ပါတယ်။
အမှားများ
အမှားများတဲ့ အချက်တွေ
- Upload လုပ်တဲ့ file၊ session ဒါမှမဟုတ် SQLite database ကို container ရဲ့ filesystem ထဲ သိမ်းတာ။ Redeploy လုပ်တိုင်း ပျောက်ပြီး instance နှစ်ခု run ရင် user က request တစ်ခုစီမှာ data မတူတာကို မြင်ရပါတယ်။
- Next.js server ရဲ့ port 3000 ကို reverse proxy မပါဘဲ internet ကို တိုက်ရိုက် ဖွင့်တာ။ HTTPS မရှိ၊ compression နဲ့ rate limiting မရှိဘဲ နှေးတဲ့ client တွေက connection တွေကို ကြာကြာ ကိုင်ထားနိုင်ပါတယ်။
- Standalone folder ကိုပဲ ကူးပြီး .next/static နဲ့ public ကို မေ့တာ။ HTML ပေါ်ပေမယ့် style မပါ၊ button တွေ မလုပ်တဲ့ page ကို ရပါတယ်။
- .dockerignore မထားဘဲ COPY . . လုပ်တာ ဒါမှမဟုတ် secret ကို Dockerfile ရဲ့ ENV နဲ့ ထည့်တာ။ နောက် layer မှာ ဖျက်လိုက်ရင်တောင် image ကို ရနိုင်သူတိုင်း secret ကို ဖတ်နိုင်သွားပါတယ်။
Mini challenge
ကိုယ်တိုင် လုပ်ကြည့်ရအောင်: liveness နဲ့ readiness health route
Container ရဲ့ HEALTHCHECK နဲ့ load balancer က ခေါ်မယ့် app/api/health/route.ts ကို ရေးပါ။ /api/health က process အသက်ရှင်ကြောင်း (liveness) ကိုပဲ ဖြေပြီး /api/health?ready=1 က database ကိုပါ ချိတ်လို့ ရကြောင်း (readiness) စစ်ပါတယ်။ Database မဖြေရင် ထာဝရ မစောင့်ဘဲ timeout နဲ့ 503 ပြန်ပေးပါ။
- Liveness က database ကို မထိဘဲ 200 { status: "ok", version } ပြန်ပေးပါ
- Readiness က 500 ms timeout နဲ့ database ကို ping ပြီး ရရင် 200 "ready"၊ မရရင် 503 "not-ready" ပြန်ပေးပါ
- version ကို run time env APP_VERSION ကနေ ယူပါ
- Readiness fail ရင် secret မပါတဲ့ JSON log တစ်ကြောင်း ရေးပါ
type Ping = () => Promise<void>;
// In the app this would run `SELECT 1` through your db module.
let pingDatabase: Ping = async () => {};
function withTimeout(task: Promise<void>, ms: number): Promise<void> {
// TODO: reject with Error("timeout") if task takes longer than ms
return task;
}
export async function GET(request: Request): Promise<Response> {
// TODO: read APP_VERSION and the ?ready=1 query parameter
// TODO: liveness: 200 { status: "ok", version }
// TODO: readiness: ping with a timeout, 200 "ready" or 503 "not-ready" + a log line
return Response.json({});
}
// TODO: probe(path) calls GET with new Request(...) and prints status + body
// TODO: probe both paths, then swap in a database that never answers and repeatရလဒ်: ပထမ probe နှစ်ခုက 200 "ok" နဲ့ 200 "ready" ပြပါတယ်။ Database ကို ဖြေမပေးတဲ့ အတုနဲ့ အစားထိုးပြီးနောက် liveness က 200 "ok" ကျန်နေပြီး readiness က readiness-failed log တစ်ကြောင်းနဲ့ 503 "not-ready" ဖြစ်ပါတယ်။ version က 1.4.0 ပါ။
အရိပ်အမြွက် ကြည့်ရန်
Promise.race([task, timeoutPromise]) က အရင်ပြီးတဲ့ promise ရဲ့ ရလဒ်ကို ယူပါတယ်။ new URL(request.url).searchParams.get("ready") နဲ့ query ကို ဖတ်ပါ။ ဘယ်တော့မှ မပြီးတဲ့ database ကို () => new Promise(() => {}) နဲ့ အတုလုပ်နိုင်ပါတယ်။
အဖြေ ကြည့်ရန်
// Liveness (/api/health) answers "is the process up?".
// Readiness (/api/health?ready=1) also asks "can it reach the database?".
type Ping = () => Promise<void>;
// In the app this would run `SELECT 1` through your db module.
let pingDatabase: Ping = async () => {};
function withTimeout(task: Promise<void>, ms: number): Promise<void> {
const timeout = new Promise<void>((_, reject) => {
setTimeout(() => reject(new Error("timeout")), ms);
});
return Promise.race([task, timeout]);
}
export async function GET(request: Request): Promise<Response> {
const version = process.env.APP_VERSION ?? "unknown";
const wantsReady = new URL(request.url).searchParams.get("ready") === "1";
if (!wantsReady) return Response.json({ status: "ok", version });
try {
await withTimeout(pingDatabase(), 500);
return Response.json({ status: "ready", version });
} catch (err) {
const reason = (err as Error).message;
console.error(JSON.stringify({ event: "readiness-failed", reason }));
return Response.json({ status: "not-ready", version }, { status: 503 });
}
}
// Demo: call the handler the way a HEALTHCHECK or load balancer would.
async function probe(path: string) {
const res = await GET(new Request(`http://localhost:3000${path}`));
console.log(`${path} -> ${res.status} ${JSON.stringify(await res.json())}`);
}
async function demo() {
process.env.APP_VERSION = "1.4.0";
await probe("/api/health");
await probe("/api/health?ready=1");
pingDatabase = () => new Promise(() => {}); // a database that never answers
await probe("/api/health");
await probe("/api/health?ready=1");
}
demo();Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- output: "standalone" က server.js နဲ့ လိုတဲ့ node_modules ကိုပဲ ထုတ်ပေးပြီး public နဲ့ .next/static ကို ကိုယ်တိုင် ကူးရပါတယ်
- Multi-stage build က install၊ build၊ run ကို ခွဲပြီး နောက်ဆုံး image ကို သေးငယ်စေပါတယ်
- NEXT_PUBLIC_ ကို build arg အဖြစ်၊ secret တွေကို run time မှာ ပေးပြီး image ထဲ မထည့်ပါနဲ့
- Non-root user၊ health check၊ reverse proxy သုံးပြီး data ကို container အပြင်မှာ သိမ်းပါ
Next.js — Self-Hosting — Next.js
Next.js — Self-Hosting — Next.js