42 lines
897 B
Docker
42 lines
897 B
Docker
# 阶段 1: 依赖安装和构建
|
|
FROM node:18-alpine AS deps
|
|
WORKDIR /app
|
|
|
|
# 复制 package.json 和 package-lock.json (如果可用)
|
|
COPY package.json package-lock.json* ./
|
|
|
|
# 安装依赖
|
|
RUN npm ci
|
|
|
|
# 阶段 2: 构建应用
|
|
FROM node:18-alpine AS builder
|
|
WORKDIR /app
|
|
COPY --from=deps /app/node_modules ./node_modules
|
|
COPY . .
|
|
|
|
# 构建应用
|
|
RUN npm run build
|
|
|
|
# 阶段 3: 运行
|
|
FROM node:18-alpine AS runner
|
|
WORKDIR /app
|
|
|
|
ENV NODE_ENV production
|
|
|
|
RUN addgroup --system --gid 1001 nodejs
|
|
RUN adduser --system --uid 1001 nextjs
|
|
|
|
COPY --from=builder /app/public ./public
|
|
|
|
# 自动利用输出跟踪来减少镜像大小
|
|
# https://nextjs.org/docs/advanced-features/output-file-tracing
|
|
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
|
|
|
|
CMD ["node", "server.js"] |