# 构建阶段
FROM docker.m.daocloud.io/node:22.10.0-alpine AS builder

# 设置工作目录
WORKDIR /app

# 设置yarn镜像源为淘宝镜像
RUN yarn config set registry https://registry.npmmirror.com && \
    yarn config set disturl https://npmmirror.com/dist --global

# 复制package.json和yarn.lock
COPY package.json yarn.lock* ./

# 安装依赖（包括devDependencies用于构建）
RUN yarn install --frozen-lockfile

# 复制源代码
COPY . .

# 注入 NEXT_PUBLIC_API_BASE_URL（Next.js NEXT_PUBLIC_* 变量必须在 build 期注入才能进客户端 JS 包）
ARG NEXT_PUBLIC_API_BASE_URL
ENV NEXT_PUBLIC_API_BASE_URL=${NEXT_PUBLIC_API_BASE_URL}

# 构建应用
RUN yarn build

# 运行阶段
FROM docker.m.daocloud.io/node:22.10.0-alpine AS runner

# 设置工作目录
WORKDIR /app

# 设置yarn镜像源为淘宝镜像
RUN yarn config set registry https://registry.npmmirror.com && \
    yarn config set disturl https://npmmirror.com/dist --global

# 复制package.json和yarn.lock
COPY package.json yarn.lock* ./

# 仅安装生产依赖
RUN yarn install --production --frozen-lockfile

# 设置环境变量
ENV NODE_ENV=production
ENV PATH=/app/node_modules/.bin:$PATH

# 从构建阶段复制构建产物
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/public ./public
COPY --from=builder /app/next.config.mjs ./

# 暴露端口
EXPOSE 3000

# 启动应用
CMD ["yarn", "start"] 