当前位置: 首页 > news >正文

NextJS 使用 Docker 发布

NextJS 开发完成之后,如果使用容器发布,需要 Dockerfile 和 修改配置文件。

Dockerfile

在官方镜像的基础上,修改为 aliyun 镜像地址,并添加 aliyun npm mirror。

FROM registry.cn-hangzhou.aliyuncs.com/leedar/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# Install dependencies based on the preferred package manager
COPY package.json yarn.lock* package-lock.json* pnpm-lock.yaml* ./
RUN npm config set registry https://registry.npmmirror.com
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# Rebuild the source code only when needed
FROM base AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .# 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 1RUN \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 /appENV NODE_ENV production
# Uncomment the following line in case you want to disable telemetry during runtime.
# ENV NEXT_TELEMETRY_DISABLED 1RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjsCOPY --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/staticUSER nextjsEXPOSE 3000ENV PORT=3000# 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

配置文件修改

修改 next.config.mjs,添加 output: ‘standalone’ ,不添加这行代码编译会报错。

/** @type {import('next').NextConfig} */
const nextConfig = {output: 'standalone'
};export default nextConfig;

http://www.mrgr.cn/news/2126.html

相关文章:

  • 算法-IMM
  • RK3568平台开发系列讲解(UART篇)line discipline
  • NIO中的异步—ChannelFuture、CloseFuture以及异步提升在NIO中的应用
  • MySQL中处理JSON数据:大数据分析的新方向,MYSQL如何处理JSON数据,参数讲解+实战案例+全网最全
  • wo是如何克服编程学习中的挫折感的?
  • roles
  • 力学笃行(五)Qt key绑定、钩子(hook)
  • Go-Zero微服务框架下开发接口流程
  • 【深度学习】OCR, 如何使用 Tesseract 进行 OCR 识别
  • spring揭秘07-aop基本要素及代理模式3种实现
  • 【C++】————智能指针
  • 赛氪网闪耀IGTA2024:为青年学者搭建平台,获科协高度评价
  • Linux信号的概念信号的产生
  • Web开发:ORM框架之Freesql的入门和技巧使用小结
  • HTTP范围放大攻击简记
  • 牛客算法小题
  • openssl查看证书公钥 openssl 验证证书和密钥
  • Linux软件编程day(12) -udp
  • 深度学习----------------------深度卷积神经网络AlexNet
  • Python3网络爬虫开发实战(9)代理的使用 (需补充代理池的构建)