FROM node:22.14-alpine AS base

# 运行时依赖
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories
RUN apk add cairo pango giflib ttf-dejavu

# 依赖缓存阶段
FROM base AS deps
WORKDIR /app

RUN apk add build-base g++ cairo-dev pango-dev giflib-dev

COPY package.json pnpm-lock.yaml* ./
RUN corepack enable pnpm
RUN pnpm config set registry https://registry.npmmirror.com
RUN pnpm install --frozen-lockfile

# 构建阶段
FROM base AS builder
WORKDIR /app

COPY --from=deps /app/node_modules ./node_modules
COPY .. .

ENV NEXT_TELEMETRY_DISABLED=1

RUN corepack enable pnpm
RUN pnpm run build

# 生产阶段
FROM base AS runner
WORKDIR /app

ENV NEXT_TELEMETRY_DISABLED=1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public
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
ENV HOSTNAME="0.0.0.0"

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