Skip to content

Commit

Permalink
feat: 优化dockerfile,可使包体积从1.1G缩小至200M
Browse files Browse the repository at this point in the history
  • Loading branch information
owenyang0 committed Nov 21, 2024
1 parent 762c9e9 commit 2e18ed2
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
28 changes: 22 additions & 6 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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"]
2 changes: 1 addition & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2e18ed2

Please sign in to comment.