简介
Next.js是React团队和Vercel正式支持的现代开源React.js框架。大多数开发人员选择通过Vercel部署Next.js,但其他平台可以使用独立的导出选项。Docker是开发、测试和运输生产软件的行业标准。它比虚拟机更轻,并且可以跨云供应商(如AWS、Digital Ocean、Azure、Vercel、Netlify等)进行移植。
步骤1:准备
将以下片段添加到yournextnext.config.js
文件中,以启用Docker的standalone
输出。
// next.config.js
module.exports = {
// ... rest of the configuration.
output: "standalone",
};
Test your application as it would be in production using next build
and running node server.js
in .next/standalone/
. This way we can catch any errors before moving our code into a production environment.
第2步:添加Docker文件
在Next.js应用程序的根目录上创建一个Dockerfile
文件,并复制以下代码。
请注意,如果您使用的是Windows,pnpm将无法正常工作,因为Docker将尝试在node_modules
文件夹中使用符号链接,因此我建议使用Yarn或NPM。
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
# 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
# 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 1
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
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
ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
CMD ["node", "server.js"]
此
Dockerfile
脚本将运行几个阶段,并适当缓存它们,以减少映像构建时间。FROM base AS deps
使用您在开发期间使用的任何软件包管理器的锁文件安装您的依赖项,FROM base AS builder
复制yournodenode_modules
并构建应用程序,FROM base as runner
将在终端服务器上执行并运行您的应用程序。您可以在这里找到一个示例项目。
第3步:构建和测试Docker映像
现在运行docker build -t {image-name} .
不要忘记.
!
在部署之前,使用正确的环境变量测试您的docker映像很重要。运行docker run -p 3000:3000 {image-name}
在localhost:3000
上测试您的应用程序。如果您想在其他端口上进行测试,请使用docker run -p {port}:3000 {image-name}
。
我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=5vbhp91f157x如果您部署到Digital Ocean,您的图像名称将看起来像
registry.digitalocean.com/your-registry/name
。有关更多详细信息,请参阅他们的docker注册表文档。
请登录后查看评论内容