-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add script & update dockerfile.
- Loading branch information
1 parent
bb0f0f2
commit f729515
Showing
6 changed files
with
161 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,7 @@ | ||
# 忽略不需要复制到Docker镜像中的文件和目录 | ||
|
||
# 忽略开发环境依赖 | ||
Dockerfile | ||
.dockerignore | ||
node_modules | ||
|
||
# 忽略构建产物 | ||
npm-debug.log | ||
README.md | ||
.next | ||
|
||
# 忽略日志文件 | ||
*.log | ||
|
||
# 忽略临时文件 | ||
*.tmp | ||
|
||
# 忽略版本控制文件 | ||
.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,72 @@ | ||
FROM node:18 | ||
FROM node:18-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 文件到工作目录 | ||
COPY package*.json ./ | ||
# Install dependencies based on the preferred package manager | ||
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ | ||
|
||
# 安装项目依赖 | ||
# RUN yarn install | ||
RUN \ | ||
if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ | ||
elif [ -f package-lock.json ]; then npm ci; \ | ||
elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ | ||
else echo "Lockfile not found." && exit 1; \ | ||
fi | ||
|
||
|
||
# Rebuild the source code only when needed | ||
FROM base AS builder | ||
WORKDIR /app | ||
|
||
|
||
COPY --from=deps /app/node_modules ./node_modules | ||
# 复制项目文件到工作目录 | ||
COPY . . | ||
|
||
# 构建 Next.js 应用 | ||
# 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 | ||
|
||
RUN yarn build | ||
|
||
# If using npm comment out above and use below instead | ||
# RUN npm run build | ||
|
||
# Production image, copy all the files and run next | ||
FROM base AS runner | ||
WORKDIR /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 | ||
|
||
# 暴露容器的 3000 端口 | ||
EXPOSE 3000 | ||
|
||
ENV PORT 3000 | ||
# set hostname to localhost | ||
ENV HOSTNAME "0.0.0.0" | ||
|
||
# 运行 Next.js 应用 | ||
CMD ["npm", "start"] | ||
CMD ["node", "server.js"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright (c) Facebook, Inc. and its affiliates. | ||
*/ | ||
|
||
/** | ||
* @type {import('next').NextConfig} | ||
**/ | ||
const nextConfig = { | ||
output: 'standalone', | ||
pageExtensions: ['jsx', 'js', 'ts', 'tsx', 'mdx', 'md'], | ||
reactStrictMode: true, | ||
experimental: { | ||
// TODO: Remove after https://github.com/vercel/next.js/issues/49355 is fixed | ||
appDir: false, | ||
scrollRestoration: true, | ||
legacyBrowsers: false, | ||
}, | ||
env: {}, | ||
webpack: (config, {dev, isServer, ...options}) => { | ||
if (process.env.ANALYZE) { | ||
const {BundleAnalyzerPlugin} = require('webpack-bundle-analyzer'); | ||
config.plugins.push( | ||
new BundleAnalyzerPlugin({ | ||
analyzerMode: 'static', | ||
reportFilename: options.isServer | ||
? '../analyze/server.html' | ||
: './analyze/client.html', | ||
}) | ||
); | ||
} | ||
|
||
// Don't bundle the shim unnecessarily. | ||
config.resolve.alias['use-sync-external-store/shim'] = 'react'; | ||
|
||
const {IgnorePlugin, NormalModuleReplacementPlugin} = require('webpack'); | ||
config.plugins.push( | ||
new NormalModuleReplacementPlugin( | ||
/^raf$/, | ||
require.resolve('./src/utils/rafShim.js') | ||
), | ||
new NormalModuleReplacementPlugin( | ||
/^process$/, | ||
require.resolve('./src/utils/processShim.js') | ||
), | ||
new IgnorePlugin({ | ||
checkResource(resource, context) { | ||
if ( | ||
/\/eslint\/lib\/rules$/.test(context) && | ||
/\.\/[\w-]+(\.js)?$/.test(resource) | ||
) { | ||
// Skips imports of built-in rules that ESLint | ||
// tries to carry into the bundle by default. | ||
// We only want the engine and the React rules. | ||
return true; | ||
} | ||
return false; | ||
}, | ||
}) | ||
); | ||
|
||
return config; | ||
}, | ||
}; | ||
|
||
module.exports = nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
const config = require('./config'); | ||
|
||
const nextConfig = { | ||
...config, | ||
output: 'standalone', | ||
} | ||
|
||
module.exports = nextConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import fs from 'node:fs/promises'; | ||
import path from 'node:path'; | ||
|
||
;(async () => { | ||
try { | ||
const nextPath = path.resolve('../next.config.js'); | ||
console.log('nextPath', nextPath); | ||
await fs.rename(nextPath, nextPath.replace('next.config.js', 'config.js')); | ||
await fs.copyFile('./next.config.js', nextPath); | ||
} catch (error) { | ||
console.error(error); | ||
} | ||
})(); |