# syntax=docker/dockerfile:1FROM node:24-alpine AS base# Bun 安装需要的工具RUN apk add --no-cache bash curl xz ca-certificates git# 安装 Bun(可通过 --build-arg 指定版本,否则装最新)ARG BUN_VERSION=ENV BUN_INSTALL=/usr/local/bunRUN bash -lc 'set -eux; v="${BUN_VERSION:+bun-v${BUN_VERSION}}"; curl -fsSL https://bun.com/install | bash -s -- ${v:-}'# 让后续阶段都能找到 bunENV PATH="${BUN_INSTALL}/bin:${PATH}"# 验证RUN bun --version && node -v && npm -v# Install dependencies only when neededFROM base AS deps# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.RUN apk add --no-cache libc6-compatWORKDIR /app# Install dependencies based on the preferred package managerCOPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* .npmrc* bun.lock* ./RUN \ if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ elif [ -f package-lock.json ]; then npm ci; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \ elif [ -f bun.lock ]; then bun i --frozen-lockfile; \ else echo "Lockfile not found." && exit 1; \ fi# Rebuild the source code only when neededFROM base AS builderWORKDIR /appCOPY --from=deps /app/node_modules ./node_modulesCOPY . .# Next.js collects completely anonymous telemetry data about general usage.# Learn more here: https://nextjs.org/telemetry# Uncomment the following line in case you want to disable telemetry during the build.ENV NEXT_TELEMETRY_DISABLED=1RUN \ if [ -f yarn.lock ]; then yarn run build; \ elif [ -f package-lock.json ]; then npm run build; \ elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \ elif [ -f bun.lock ]; then bun run build; \ else echo "Lockfile not found." && exit 1; \ fi# Production image, copy all the files and run nextFROM base AS runnerWORKDIR /appENV NODE_ENV=production# Uncomment the following line in case you want to disable telemetry during runtime.ENV NEXT_TELEMETRY_DISABLED=1RUN addgroup --system --gid 1001 nodejsRUN adduser --system --uid 1001 nextjsCOPY --from=builder /app/public ./public# Automatically leverage output traces to reduce image size# https://nextjs.org/docs/advanced-features/output-file-tracingCOPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/staticUSER nextjsEXPOSE 3000ENV PORT=3000# server.js is created by next build from the standalone output# https://nextjs.org/docs/pages/api-reference/config/next-config-js/outputENV HOSTNAME="0.0.0.0"CMD ["node", "server.js"]