Thuta Learning
AdvancedWeb Developmentintermediate

Self-Hosting with Docker

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Enable standalone output
  • Build a production Docker image
  • Think through the reverse proxy and persistent services

You might want to skip Vercel and deploy to your own server, Kubernetes, or a cloud container service instead. Self-hosting gives you more freedom, but you're on the hook for the reverse proxy, process restarts, cache coordination, and security patches yourself.

The key idea

Enabling output: standalone in next.config.ts produces a separate output with only the files needed at runtime. A multi-stage Docker build separates dependency install, build, and runtime, keeping the final image small. The official guide recommends putting the Next.js server behind nginx or a cloud load balancer in production rather than exposing it directly to the internet. If you run multiple instances, you'll also need to plan for cache and invalidation coordination.

Let's try it together

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"]

How the code works

The builder stage builds the app, and the runner stage takes only the standalone output and public assets. The container runs server.js on port 3000. Don't store the database or uploads permanently on the container's filesystem.

You should see
You can build a production Docker image running a standalone Next.js server.

5-Minute Try-It

Draw a deployment diagram that includes a Docker health check, a non-root user, and HTTPS on the reverse proxy.

Next.js — Self-HostingNext.js

Easy traps

  • Exposing the Next.js server directly to the internet without a reverse proxy
  • Storing uploaded files or sessions permanently on the container's local filesystem or memory

Exercise

Draw a deployment diagram that includes a Docker health check, a non-root user, and HTTPS on the reverse proxy.

You'll know it worked when: You can build a production Docker image running a standalone Next.js server.

Self-Hosting with Docker | Thuta Learning