-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDockerfile
More file actions
95 lines (72 loc) · 2.83 KB
/
Dockerfile
File metadata and controls
95 lines (72 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
##### deps #####
FROM node:20-bookworm AS deps
WORKDIR /app
# 配置 Debian 国内镜像源(加速包下载)
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources \
&& sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
# 启用 corepack 并配置 yarn
RUN corepack enable \
&& corepack prepare yarn@1.22.22 --activate \
&& yarn config set registry https://registry.npmmirror.com
# 复制依赖文件
COPY package.json yarn.lock ./
# 安装依赖(忽略脚本,避免在构建阶段执行 postinstall)
RUN yarn install --frozen-lockfile --ignore-scripts
##### builder #####
FROM deps AS builder
# 只复制构建 Next.js 应用需要的文件,而不是整个项目
# Next.js 构建需要:源代码、配置文件、Prisma schema
COPY src ./src
COPY public ./public
COPY prisma ./prisma
COPY next.config.ts ./
COPY postcss.config.mjs ./
COPY tsconfig.json ./
COPY package.json yarn.lock ./
# 生成 Prisma Client 并构建 Next.js 应用
# 注意:使用 lazyConnect 和延迟加载,构建时不会连接外部服务
RUN yarn prisma generate \
&& yarn build
##### runner #####
FROM node:20-bookworm-slim AS runner
WORKDIR /app
ENV NODE_ENV=production \
NODE_OPTIONS=--max-old-space-size=2048 \
NEXT_TELEMETRY_DISABLED=1
# 配置 Debian 国内镜像源(加速包下载)
RUN sed -i 's/deb.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources \
&& sed -i 's/security.debian.org/mirrors.aliyun.com/g' /etc/apt/sources.list.d/debian.sources
# 安装 dumb-init 和 curl(用于健康检查)
RUN apt-get update \
&& apt-get install -y --no-install-recommends \
dumb-init \
ca-certificates \
curl \
&& rm -rf /var/lib/apt/lists/*
# 创建非 root 用户
RUN groupadd --system --gid 1001 nodejs \
&& useradd --system --uid 1001 nextjs
# 复制 package.json 和 yarn.lock
COPY package.json yarn.lock ./
# 启用 corepack 并安装生产依赖
RUN corepack enable \
&& corepack prepare yarn@1.22.22 --activate \
&& yarn config set registry https://registry.npmmirror.com \
&& yarn install --frozen-lockfile --ignore-scripts --production=true \
&& yarn cache clean
# 从 builder 阶段复制构建产物
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
COPY --from=builder --chown=nextjs:nodejs /app/public ./public
# 复制 Prisma schema 和生成的 Client(用于运行时数据库操作)
COPY --from=builder --chown=nextjs:nodejs /app/prisma ./prisma
COPY --from=builder --chown=nextjs:nodejs /app/node_modules/.prisma ./node_modules/.prisma
# 切换到非 root 用户
USER nextjs
# 暴露端口
EXPOSE 3000
ENV PORT=3000 \
HOSTNAME="0.0.0.0"
# 使用 dumb-init 启动应用
ENTRYPOINT ["dumb-init", "--"]
CMD ["node", "server.js"]