From 2e18ed2e0effd0f6fa3161db074ac936cbb15205 Mon Sep 17 00:00:00 2001 From: yangsong13 Date: Thu, 21 Nov 2024 17:35:43 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E4=BC=98=E5=8C=96dockerfile,=E5=8F=AF?= =?UTF-8?q?=E4=BD=BF=E5=8C=85=E4=BD=93=E7=A7=AF=E4=BB=8E1.1G=E7=BC=A9?= =?UTF-8?q?=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