Files
SaaS2/Dockerfile
RUI 533d2862ff
Some checks failed
Next.js CI/CD 流水线 / deploy (push) Failing after 1m40s
0606.6
2025-06-06 02:01:55 +08:00

72 lines
1.8 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 pnpm (Ubuntu环境解决兼容性)
# 第一阶段:构建阶段
FROM node:22-slim AS builder
# 设置工作目录
WORKDIR /app
# 安装必要的系统依赖
RUN apt-get update && apt-get install -y \
python3 \
make \
g++ \
&& rm -rf /var/lib/apt/lists/*
# 安装 pnpm
RUN npm install -g pnpm@9.15.0
# 复制包管理器配置文件 (只需package.jsonpnpm会自动生成锁文件)
COPY package.json ./
# 安装依赖 (pnpm正确处理二进制文件自动生成锁文件)
RUN pnpm install --prod --no-optional && \
pnpm store prune
# 复制源代码和配置文件
COPY . .
# 重新安装所有依赖用于构建 (包含开发依赖)
RUN pnpm install --no-optional
# 构建应用
RUN pnpm run build
# 清理构建阶段缓存 (关键优化点)
RUN rm -rf node_modules && \
rm -rf ~/.pnpm-store && \
rm -rf /root/.pnpm-store && \
pnpm store prune
# 第二阶段:运行阶段 (使用Ubuntu slim镜像)
FROM node:22-slim AS runner
# 设置工作目录
WORKDIR /app
# 创建非root用户
RUN groupadd --system --gid 1001 nodejs && \
useradd --system --uid 1001 --gid nodejs nextjs
# 只安装必要的运行时工具
RUN apt-get update && apt-get install -y \
wget \
&& rm -rf /var/lib/apt/lists/*
# 从构建阶段复制构建产物 (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"]