Skip to content

Commit

Permalink
fix: config & Dockerfile (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
whes1015 authored Nov 5, 2024
2 parents 287b06e + 86248c2 commit e352f58
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 18 deletions.
73 changes: 55 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
# 選擇 Node.js 的基礎映像檔
FROM node:lts AS builder
FROM node:20-alpine AS base

# 設定工作目錄
# 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

# 複製 package.json 和 package-lock.json (或 yarn.lock)
COPY package*.json ./
# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN \
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \
elif [ -f package-lock.json ]; then npm ci; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm i --frozen-lockfile; \
else echo "Lockfile not found." && exit 1; \
fi

# 安裝依賴
RUN npm i

# 複製其他應用程式文件
# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .

RUN npm run build
# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry during the build.
# ENV NEXT_TELEMETRY_DISABLED 1

# 選擇第二階段構建以優化大小
FROM node:lts-alpine
RUN \
if [ -f yarn.lock ]; then yarn run build; \
elif [ -f package-lock.json ]; then npm run build; \
elif [ -f pnpm-lock.yaml ]; then corepack enable pnpm && pnpm run build; \
else echo "Lockfile not found." && exit 1; \
fi

# 設定工作目錄
# Production image, copy all the files and run next
FROM base AS runner
WORKDIR /app

# 複製從第一階段構建的 node_modules 和應用文件
COPY --from=builder /app .
ENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1

RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

COPY --from=builder /app/public ./public

# Set the correct permission for prerender cache
RUN mkdir .next
RUN chown nextjs:nodejs .next

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static

USER nextjs

EXPOSE 3000

# 暴露應用程式使用的端口
# EXPOSE 1015
ENV PORT 3000

# 定義啟動命令
CMD ["npm", "run", "start"]
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
# CMD HOSTNAME="0.0.0.0" node server.js
CMD node server.js
1 change: 1 addition & 0 deletions next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const nextConfig = withBundleAnalyzer(withNextIntl({
fullUrl: true,
},
},
output: "standalone",
}))

module.exports = nextConfig

0 comments on commit e352f58

Please sign in to comment.