|
1 | 1 | ---
|
2 |
| -title: React Next |
3 |
| -date: 2020-10-17 |
| 2 | +title: React Next docker部署 |
| 3 | +date: 2023-12-06 |
4 | 4 | sidebar: auto
|
5 | 5 | categories:
|
6 | 6 | - React
|
7 | 7 | - 前端
|
8 | 8 | tags:
|
9 | 9 | - JS
|
10 | 10 | - React
|
11 |
| -- 服务端渲染 |
| 11 | +- ssr |
| 12 | + |
12 | 13 | ---
|
| 14 | +## next.config.js配置: |
| 15 | +```js |
| 16 | +const nextConfig = { |
| 17 | + output: 'standalone' |
| 18 | + // ... |
| 19 | +} |
| 20 | +``` |
| 21 | +## docker部署 |
| 22 | +```dockerfile |
| 23 | +FROM node:18-alpine AS base |
| 24 | + |
| 25 | +# Install dependencies only when needed |
| 26 | +FROM base AS deps |
| 27 | +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. |
| 28 | +RUN apk add --no-cache libc6-compat |
| 29 | +WORKDIR /app |
| 30 | + |
| 31 | +# Install dependencies based on the preferred package manager |
| 32 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ |
| 33 | +RUN \ |
| 34 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 35 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 36 | + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ |
| 37 | + else echo "Lockfile not found." && exit 1; \ |
| 38 | + fi |
| 39 | + |
| 40 | + |
| 41 | +# Rebuild the source code only when needed |
| 42 | +FROM base AS builder |
| 43 | +WORKDIR /app |
| 44 | +COPY --from=deps /app/node_modules ./node_modules |
| 45 | +COPY . . |
| 46 | + |
| 47 | +# Next.js collects completely anonymous telemetry data about general usage. |
| 48 | +# Learn more here: https://nextjs.org/telemetry |
| 49 | +# Uncomment the following line in case you want to disable telemetry during the build. |
| 50 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 51 | + |
| 52 | +RUN npm run build |
| 53 | + |
| 54 | +# Production image, copy all the files and run next |
| 55 | +FROM base AS runner |
| 56 | +WORKDIR /app |
| 57 | + |
| 58 | +ENV NODE_ENV production |
| 59 | +# Uncomment the following line in case you want to disable telemetry during runtime. |
| 60 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 61 | + |
| 62 | +COPY --from=builder /app/public ./public |
| 63 | + |
| 64 | +# Set the correct permission for prerender cache |
| 65 | +RUN addgroup -g 1001 -S nodejs |
| 66 | +RUN adduser -S nextjs -u 1001 |
| 67 | + |
| 68 | +# Automatically leverage output traces to reduce image size |
| 69 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 70 | +# 只有在standalone模式下build才会生成standalone文件夹 |
| 71 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 72 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 73 | + |
| 74 | +USER nextjs |
| 75 | + |
| 76 | +EXPOSE 8080 |
| 77 | + |
| 78 | +ENV PORT 8080 |
| 79 | +# set hostname to localhost |
| 80 | +ENV HOSTNAME "0.0.0.0" |
| 81 | + |
| 82 | +# server.js is created by next build from the standalone output |
| 83 | +# https://nextjs.org/docs/pages/api-reference/next-config-js/output |
| 84 | +CMD ["node", "server.js"] |
| 85 | +``` |
| 86 | + |
| 87 | + |
| 88 | +## docker使用pm2: |
| 89 | +根目录下新增`ecosystem.config.js`: |
| 90 | +```js |
| 91 | +module.exports = { |
| 92 | + apps: [ |
| 93 | + { |
| 94 | + name: 'next-docker-pm2', |
| 95 | + exec_mode: 'cluster', |
| 96 | + instances: 'max', |
| 97 | + script: "server.js", // 启动脚本 |
| 98 | + }, |
| 99 | + ], |
| 100 | +} |
| 101 | + |
| 102 | +``` |
| 103 | +修改`dockerfile`: |
| 104 | +```dockerfile |
| 105 | +FROM node:18-alpine AS base |
| 106 | + |
| 107 | +# Install dependencies only when needed |
| 108 | +FROM base AS deps |
| 109 | +# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed. |
| 110 | +RUN apk add --no-cache libc6-compat |
| 111 | +WORKDIR /app |
| 112 | + |
| 113 | +# Install dependencies based on the preferred package manager |
| 114 | +COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./ |
| 115 | +RUN \ |
| 116 | + if [ -f yarn.lock ]; then yarn --frozen-lockfile; \ |
| 117 | + elif [ -f package-lock.json ]; then npm ci; \ |
| 118 | + elif [ -f pnpm-lock.yaml ]; then yarn global add pnpm && pnpm i --frozen-lockfile; \ |
| 119 | + else echo "Lockfile not found." && exit 1; \ |
| 120 | + fi |
| 121 | + |
| 122 | + |
| 123 | +# Rebuild the source code only when needed |
| 124 | +FROM base AS builder |
| 125 | +WORKDIR /app |
| 126 | +COPY --from=deps /app/node_modules ./node_modules |
| 127 | +COPY . . |
| 128 | + |
| 129 | +# Next.js collects completely anonymous telemetry data about general usage. |
| 130 | +# Learn more here: https://nextjs.org/telemetry |
| 131 | +# Uncomment the following line in case you want to disable telemetry during the build. |
| 132 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 133 | + |
| 134 | +RUN npm run build |
| 135 | + |
| 136 | +# Production image, copy all the files and run next |
| 137 | +FROM base AS runner |
| 138 | +WORKDIR /app |
| 139 | + |
| 140 | +ENV NODE_ENV production |
| 141 | +# Uncomment the following line in case you want to disable telemetry during runtime. |
| 142 | +# ENV NEXT_TELEMETRY_DISABLED 1 |
| 143 | + |
| 144 | +COPY --from=builder /app/public ./public |
| 145 | + |
| 146 | +# Set the correct permission for prerender cache |
| 147 | +RUN addgroup -g 1001 -S nodejs |
| 148 | +RUN adduser -S nextjs -u 1001 |
| 149 | + |
| 150 | +# Automatically leverage output traces to reduce image size |
| 151 | +# https://nextjs.org/docs/advanced-features/output-file-tracing |
| 152 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./ |
| 153 | +COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static |
| 154 | + |
| 155 | +# pm2 |
| 156 | +COPY --from=builder --chown=nextjs:nodejs /app/ecosystem.config.js ./ |
| 157 | +RUN npm install -g pm2 |
| 158 | + |
| 159 | +USER nextjs |
| 160 | + |
| 161 | +EXPOSE 8080 |
13 | 162 |
|
14 |
| -<iframe importance="low" height="600" width="100%;" scrolling="no" title="react lifecycle" src="https://nextjs.frontendx.cn/" loading="lazy" allowtransparency="true" allowfullscreen="true"></iframe> |
| 163 | +ENV PORT 8080 |
| 164 | +# set hostname to localhost |
| 165 | +ENV HOSTNAME "0.0.0.0" |
15 | 166 |
|
16 |
| -[如果加载失败CLICK ME](https://nextjs.frontendx.cn/) |
| 167 | +# server.js is created by next build from the standalone output |
| 168 | +# https://nextjs.org/docs/pages/api-reference/next-config-js/output |
| 169 | +# Launch app with PM2 |
| 170 | +CMD ["pm2-runtime", "ecosystem.config.js"] |
| 171 | +``` |
0 commit comments