# Dockerfile for Recipe Translation Tool
# Stage 1: Builder
FROM node:20-alpine AS builder

WORKDIR /app

# 国内镜像加速
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

COPY package.json pnpm-lock.yaml ./

# 国内源 + 快速启用 pnpm
RUN corepack enable && \
    npm config set registry https://registry.npmmirror.com && \
    pnpm config set registry https://registry.npmmirror.com

RUN pnpm install --frozen-lockfile

COPY . .

RUN pnpm build

# Stage 2: Runner
FROM node:20-alpine AS runner

WORKDIR /app

ENV NODE_ENV=production

# 国内镜像
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories

# 创建用户
RUN addgroup --system --gid 1001 nodejs && \
    adduser --system --uid 1001 nextjs

# 复制构建产物
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static

# ===================== 修复权限错误 =====================
# 复制项目根目录的配置文件
COPY --from=builder /app/config.json ./

# 给 nextjs 用户授权整个目录
RUN chown -R nextjs:nodejs /app
# ========================================================

USER nextjs

EXPOSE 3000
ENV PORT=3000
ENV HOSTNAME="0.0.0.0"

CMD ["node", "server.js"]