Files
personal/Dockerfile
T
virtheart 3c766a4967 feat: Next.js 部署形态切换: standalone Dockerfile + .dockerignore
- next.config.ts 开启 output: standalone, 构建产物 .next/standalone 自带
  精简 node_modules 与 server.js(替换原 Nuxt .output 形态)
- Dockerfile: 两阶段构建(DaoCloud node:22-slim + pnpm npmmirror),
  运行时仅拷 standalone 产物 + static + public, 非 root 用户,
  ENV HOSTNAME=0.0.0.0 PORT=3000, CMD node server.js
- .dockerignore: 排除 node_modules/.next/haoqi_clone/.workbuddy 等,
  控制构建上下文体积
2026-09-01 00:29:33 +08:00

47 lines
2.4 KiB
Docker
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# 注意:不用 "# syntax=docker/dockerfile:1" 指令(会去 Docker Hub 拉取 frontend 镜像,国内不可达)
# =========================================================
# vhsys-official-ui 企业官网前端镜像(Next.js 16,standalone Node 服务)
#
# 域名: virtheart.com + www.virtheart.com
# 构建: pnpm install → next build(output: "standalone")→ .next/standalone
# 运行: node server.js,监听 3000(PORT 由 deploy yaml 注入;
# 绑定地址由 HOSTNAME 控制,Next 缺省即 0.0.0.0,deploy yaml 兜底注入)
# =========================================================
# ---------- 阶段一:构建 ----------
# 基础镜像走 DaoCloud 国内镜像源(Docker Hub 在国内不可达)
FROM docker.m.daocloud.io/library/node:22-slim AS builder
WORKDIR /app
# npm / pnpm 使用国内镜像源(registry.npmmirror.com),否则境内拉依赖极慢
RUN npm config set registry https://registry.npmmirror.com \
&& npm install -g pnpm@11.18.0 \
&& pnpm config set registry https://registry.npmmirror.com
# 先拷贝依赖清单(含 pnpm-workspace.yaml,pnpm 11 的 allowBuilds 配置在其中),
# 最大化利用镜像层缓存
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml ./
# 允许依赖构建脚本:@next/swc 等 platform 包经 optionalDependencies 预编译分发,
# 无需项目源码即可完成安装(对比 Nuxt:无 prepare 步骤依赖源码)
RUN pnpm install --frozen-lockfile
# 拷贝源码并构建 standalone 产物到 .next/standalone/
COPY . .
ENV NEXT_TELEMETRY_DISABLED=1
RUN pnpm build
# ---------- 阶段二:运行时(Next standalone Node 服务,精简镜像 + 非 root) ----------
FROM docker.m.daocloud.io/library/node:22-slim
WORKDIR /app
RUN groupadd -r nextjs && useradd -r -g nextjs -m nextjs
# standalone 自带运行时所需的最小 node_modules 与 server.js;
# 静态资源与 public 需另行拷入(Next standalone 的官方约定)
COPY --from=builder --chown=nextjs:nextjs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nextjs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nextjs /app/public ./public
USER nextjs
ENV NODE_ENV=production \
NEXT_TELEMETRY_DISABLED=1 \
HOSTNAME=0.0.0.0 \
PORT=3000
# HOSTNAME/PORT 亦可由 deploy yaml 覆盖注入(Next 读取这两个变量绑定地址端口)
EXPOSE 3000
CMD ["node", "server.js"]