1- FROM node:22.12.0-alpine3.19 AS builder
1+ # # exmaple: https://github.com/vercel/next.js/blob/canary/examples/with-docker/Dockerfile
2+ FROM node:22.12.0-alpine3.19 AS base
3+
4+ # Install dependencies only when needed
5+ FROM base AS deps
6+ # Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
7+ RUN apk add --no-cache libc6-compat
28WORKDIR /app
39
4- COPY package*.json ./
10+ # set mirror for npm
11+ # RUN npm config set registry https://registry.npmmirror.com
12+
13+ # RUN apt-get update && apt-get install -y python3 python3-pip build-essential
14+ # RUN ln -s /usr/bin/python3 /usr/bin/python
515
6- RUN npm ci
16+ # Install dependencies based on the preferred package manager
17+ COPY ./package*.json ./
18+ RUN npm install
19+
20+ # Rebuild the source code only when needed
21+ FROM base AS builder
22+ WORKDIR /app
23+ COPY --from=deps /app/node_modules ./node_modules
24+ COPY ./ .
725
8- COPY . .
26+ # Next.js collects completely anonymous telemetry data about general usage.
27+ # Learn more here: https://nextjs.org/telemetry
28+ # Uncomment the following line in case you want to disable telemetry during the build.
29+ ENV NEXT_TELEMETRY_DISABLED=1
930
1031RUN npm run build
1132
33+ # Production image, copy all the files and run next
34+ FROM base AS runner
35+ WORKDIR /app
1236
37+ ENV NODE_ENV=production
38+ # Uncomment the following line in case you want to disable telemetry during runtime.
39+ ENV NEXT_TELEMETRY_DISABLED=1
1340
41+ RUN addgroup --system --gid 1001 nodejs
42+ RUN adduser --system --uid 1001 nextjs
1443
44+ COPY --from=builder /app/public ./public
1545
16- # 使用轻量级的 Node.js 镜像
17- FROM node:22.12.0-alpine3.19 AS runner
46+ # Set the correct permission for prerender cache
47+ RUN mkdir .next
48+ RUN chown nextjs:nodejs .next
1849
19- # 设置工作目录
20- WORKDIR /app
50+ # Automatically leverage output traces to reduce image size
51+ # https://nextjs.org/docs/advanced-features/output-file-tracing
52+ COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
53+ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
2154
22- # 复制构建好的文件
23- COPY --from=builder /app ./
55+ USER nextjs
2456
25- # 暴露应用运行的端口
2657EXPOSE 3000
2758
28- # 启动应用
29- CMD ["npm" , "start" ]
59+ ENV PORT=3000
60+
61+ # server.js is created by next build from the standalone output
62+ # https://nextjs.org/docs/pages/api-reference/next-config-js/output
63+ ENV HOSTNAME="0.0.0.0"
64+ CMD ["node" , "server.js" ]
0 commit comments