From 2e18ed2e0effd0f6fa3161db074ac936cbb15205 Mon Sep 17 00:00:00 2001 From: yangsong13 Date: Thu, 21 Nov 2024 17:35:43 +0800 Subject: [PATCH 1/3] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96dockerfile,?= =?UTF-8?q?=E5=8F=AF=E4=BD=BF=E5=8C=85=E4=BD=93=E7=A7=AF=E4=BB=8E1.1G?= =?UTF-8?q?=E7=BC=A9=E5=B0=8F=E8=87=B3200M?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 28 ++++++++++++++++++++++------ next.config.js | 2 +- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/Dockerfile b/Dockerfile index 12650294..d08b984b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,23 +1,39 @@ ARG NOTION_PAGE_ID ARG NEXT_PUBLIC_THEME -# Install dependencies only when needed -FROM node:18-alpine3.18 AS deps +FROM node:18-alpine3.18 AS base + +# 1. Install dependencies only when needed +FROM 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-compat WORKDIR /app COPY package.json ./ RUN yarn install --frozen-lockfile -# Rebuild the source code only when needed -FROM node:18-alpine3.18 AS builder +# 2. Rebuild the source code only when needed +FROM base AS builder ARG NOTION_PAGE_ID WORKDIR /app COPY --from=deps /app/node_modules ./node_modules COPY . . RUN yarn build -ENV NODE_ENV production +# 3. Production image, copy all the files and run next +FROM base AS runner +WORKDIR /app + +ENV NODE_ENV=production + +COPY --from=builder /app/public ./public + +# Automatically leverage output traces to reduce image size +# https://nextjs.org/docs/advanced-features/output-file-tracing +COPY --from=builder /app/.next/standalone ./ +COPY --from=builder /app/.next/static ./.next/static + +# 个人仓库把将配置好的.env.local文件放到项目根目录,可自动使用环境变量 +# COPY --from=builder /app/.env.local ./ EXPOSE 3000 @@ -26,4 +42,4 @@ EXPOSE 3000 # Uncomment the following line in case you want to disable telemetry. # ENV NEXT_TELEMETRY_DISABLED 1 -CMD ["yarn", "start"] +CMD ["node", "server.js"] \ No newline at end of file diff --git a/next.config.js b/next.config.js index 585c5435..972e85fb 100644 --- a/next.config.js +++ b/next.config.js @@ -84,7 +84,7 @@ const nextConfig = { eslint: { ignoreDuringBuilds: true }, - output: process.env.EXPORT ? 'export' : undefined, + output: process.env.EXPORT ? 'export' : 'standalone', staticPageGenerationTimeout: 120, // 多语言, 在export时禁用 i18n: process.env.EXPORT From f10dfb817262bb8cd42ea2151b3a441e8e504d96 Mon Sep 17 00:00:00 2001 From: yangsong13 Date: Thu, 21 Nov 2024 20:06:58 +0800 Subject: [PATCH 2/3] =?UTF-8?q?feat:=20=E4=BB=85=E5=AF=B9NEXT=5FBUILD=5FST?= =?UTF-8?q?ANDALONE=3Dtrue=E7=94=9F=E6=95=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env.example | 1 + Dockerfile | 6 +++++- next.config.js | 2 +- 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index eb29e18c..58d0b56f 100644 --- a/.env.example +++ b/.env.example @@ -173,3 +173,4 @@ # ENABLE_CACHE= # VERCEL_ENV= # NEXT_PUBLIC_VERSION= +# NEXT_BUILD_STANDALONE= diff --git a/Dockerfile b/Dockerfile index d08b984b..01185002 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,16 +14,20 @@ RUN yarn install --frozen-lockfile # 2. Rebuild the source code only when needed FROM base AS builder ARG NOTION_PAGE_ID +ENV NEXT_BUILD_STANDALONE=true + WORKDIR /app + COPY --from=deps /app/node_modules ./node_modules COPY . . RUN yarn build # 3. Production image, copy all the files and run next FROM base AS runner +ENV NODE_ENV=production + WORKDIR /app -ENV NODE_ENV=production COPY --from=builder /app/public ./public diff --git a/next.config.js b/next.config.js index 972e85fb..658001ed 100644 --- a/next.config.js +++ b/next.config.js @@ -84,7 +84,7 @@ const nextConfig = { eslint: { ignoreDuringBuilds: true }, - output: process.env.EXPORT ? 'export' : 'standalone', + output: process.env.EXPORT ? 'export' : process.env.NEXT_BUILD_STANDALONE === 'true' ? 'standalone' : undefined, staticPageGenerationTimeout: 120, // 多语言, 在export时禁用 i18n: process.env.EXPORT From 7d7718d97181d87b6dae6d731c109c7e874bb49b Mon Sep 17 00:00:00 2001 From: yangsong13 Date: Thu, 21 Nov 2024 20:11:49 +0800 Subject: [PATCH 3/3] feat: remove extra line --- Dockerfile | 1 - 1 file changed, 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 01185002..2feec942 100644 --- a/Dockerfile +++ b/Dockerfile @@ -28,7 +28,6 @@ ENV NODE_ENV=production WORKDIR /app - COPY --from=builder /app/public ./public # Automatically leverage output traces to reduce image size