Skip to content

Commit 2e18ed2

Browse files
committed
feat: 优化dockerfile,可使包体积从1.1G缩小至200M
1 parent 762c9e9 commit 2e18ed2

File tree

2 files changed

+23
-7
lines changed

2 files changed

+23
-7
lines changed

Dockerfile

+22-6
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,39 @@
11
ARG NOTION_PAGE_ID
22
ARG NEXT_PUBLIC_THEME
33

4-
# Install dependencies only when needed
5-
FROM node:18-alpine3.18 AS deps
4+
FROM node:18-alpine3.18 AS base
5+
6+
# 1. Install dependencies only when needed
7+
FROM base AS deps
68
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
79
RUN apk add --no-cache libc6-compat
810
WORKDIR /app
911
COPY package.json ./
1012
RUN yarn install --frozen-lockfile
1113

12-
# Rebuild the source code only when needed
13-
FROM node:18-alpine3.18 AS builder
14+
# 2. Rebuild the source code only when needed
15+
FROM base AS builder
1416
ARG NOTION_PAGE_ID
1517
WORKDIR /app
1618
COPY --from=deps /app/node_modules ./node_modules
1719
COPY . .
1820
RUN yarn build
1921

20-
ENV NODE_ENV production
22+
# 3. Production image, copy all the files and run next
23+
FROM base AS runner
24+
WORKDIR /app
25+
26+
ENV NODE_ENV=production
27+
28+
COPY --from=builder /app/public ./public
29+
30+
# Automatically leverage output traces to reduce image size
31+
# https://nextjs.org/docs/advanced-features/output-file-tracing
32+
COPY --from=builder /app/.next/standalone ./
33+
COPY --from=builder /app/.next/static ./.next/static
34+
35+
# 个人仓库把将配置好的.env.local文件放到项目根目录,可自动使用环境变量
36+
# COPY --from=builder /app/.env.local ./
2137

2238
EXPOSE 3000
2339

@@ -26,4 +42,4 @@ EXPOSE 3000
2642
# Uncomment the following line in case you want to disable telemetry.
2743
# ENV NEXT_TELEMETRY_DISABLED 1
2844

29-
CMD ["yarn", "start"]
45+
CMD ["node", "server.js"]

next.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const nextConfig = {
8484
eslint: {
8585
ignoreDuringBuilds: true
8686
},
87-
output: process.env.EXPORT ? 'export' : undefined,
87+
output: process.env.EXPORT ? 'export' : 'standalone',
8888
staticPageGenerationTimeout: 120,
8989
// 多语言, 在export时禁用
9090
i18n: process.env.EXPORT

0 commit comments

Comments
 (0)