Files
SaaS2/Dockerfile
RUI 218812b768
Some checks failed
Next.js CI/CD 流水线 / deploy (push) Failing after 12s
0606.5
2025-06-06 01:46:34 +08:00

62 lines
1.5 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.
# 🚀 超优化版 Dockerfile for Next.js with npm (目标: <150MB)
# 第一阶段:构建阶段
FROM node:22-alpine AS builder
# 设置工作目录
WORKDIR /app
# 复制包管理器配置文件 (利用Docker缓存)
COPY package.json package-lock.json* ./
# 安装依赖 (使用npm禁用可选依赖)
RUN npm ci --only=production --no-optional && \
npm cache clean --force
# 复制源代码和配置文件
COPY . .
# 重新安装所有依赖用于构建
RUN npm ci --no-optional
# 构建应用
RUN npm run build
# 清理构建阶段缓存 (关键优化点)
RUN rm -rf node_modules && \
rm -rf ~/.npm && \
rm -rf /root/.npm && \
npm cache clean --force
# 第二阶段:运行阶段 (使用更小的基础镜像)
FROM node:22-alpine AS runner
# 设置工作目录
WORKDIR /app
# 创建非root用户
RUN addgroup --system --gid 1001 nodejs && \
adduser --system --uid 1001 nextjs
# 只安装必要的运行时工具
RUN apk add --no-cache wget && \
apk cache clean && \
rm -rf /var/cache/apk/*
# 从构建阶段复制构建产物 (standalone模式包含所有需要的依赖)
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
# 设置用户
USER nextjs
# 暴露端口
EXPOSE 3000
# 设置环境变量
ENV PORT=3000 \
HOSTNAME="0.0.0.0" \
NODE_ENV=production
# 启动应用 (standalone模式直接运行server.js)
CMD ["node", "server.js"]