如何使用Docker部署Next.js应用程序

简介

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.

在您的开发系统上安装Docker。

第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}

如果您部署到Digital Ocean,您的图像名称将看起来像registry.digitalocean.com/your-registry/name。有关更多详细信息,请参阅他们的docker注册表文档

THE END
喜欢就支持一下吧
点赞11 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容