我有一个基本的 nextjs 14 应用程序,它使用 clerk auth,当我部署它时,该应用程序无法启动。
我从 azure devops 的基本部署管道开始,将其部署到 azure web 应用程序中,该应用程序未能开始看到模糊的错误,我对应用程序进行了 docker 化,认为 azure 部署 Web 应用程序 (linux) 的方式可能存在问题。
我仍然面临与 dockerized 应用程序相同的问题,它无法启动。
我的日志显示以下错误代码,无法联系职员。
2024-11-15T06:42:50.8240550Z Failed to proxy http://169.254.137.3:3000/clerk_1731652970815 Error: connect ECONNRESET 169.254.137.3:3000
2024-11-15T06:42:50.8242320Z at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
2024-11-15T06:42:50.8242359Z at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
2024-11-15T06:42:50.8242390Z errno: -104,
2024-11-15T06:42:50.8242415Z code: 'ECONNRESET',
2024-11-15T06:42:50.8242438Z syscall: 'connect',
2024-11-15T06:42:50.8242465Z address: '169.254.137.3',
2024-11-15T06:42:50.8242487Z port: 3000
2024-11-15T06:42:50.8242510Z }
2024-11-15T06:42:50.8290774Z Error: connect ECONNRESET 169.254.137.3:3000
2024-11-15T06:42:50.8291242Z at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1605:16)
2024-11-15T06:42:50.8291281Z at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
2024-11-15T06:42:50.8291308Z errno: -104,
2024-11-15T06:42:50.8291333Z code: 'ECONNRESET',
2024-11-15T06:42:50.8291355Z syscall: 'connect',
2024-11-15T06:42:50.8291384Z address: '169.254.137.3',
2024-11-15T06:42:50.8291417Z port: 3000
2024-11-15T06:42:50.8291439Z }
我的dockerfile看起来像是Nextjs提供的dockerfile模板
FROM node:21-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* .npmrc* ./
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 . .
# Ensure the `generated` folder is included in the build context
# 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=development
# 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
# server.js is created by next build from the standalone output
# https://nextjs.org/docs/pages/api-reference/next-config-js/output
ENV HOSTNAME="0.0.0.0"
CMD ["node", "server.js"]
发生
ECONNRESET
错误是因为连接正在关闭,因为它的目标是本地地址 169.254.137.3
而不是 Azure Web App。
我使用 Clerk 身份验证创建了一个示例 Next.js 应用程序,并通过容器注册表将其部署到 Azure Web App。
我使用 Clerk 网站上提供的基本代码将 Clerk 与 Next.js 应用程序集成。
下面是我用来将 Next.js 应用程序推送到容器的 Dockerfile。
Dockerfile:
FROM node:18 AS base
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm install --frozen-lockfile
COPY . .
RUN npm run build
EXPOSE 3000
CMD ["npm", "start"]
我运行了以下命令来在本地 docker 容器中构建并运行应用程序 t。
docker build -t <ContainerImageName> .
docker run -p <port>:<port> --env-file .env.local <ContainerImageName>
我运行了以下命令将映像推送到 Azure 容器注册表:
az login
az acr login -n <ContainerUserName>
docker tag <ImageName> <AzureContainerRegistryName>.azurecr.io/<imageName>:<tagName>
docker push <AzureContainerRegistryName>.azurecr.io/<ImageName>:<tagName>
在创建 Azure Web 应用程序时,我为“发布”设置选择了“容器”选项,如下所示。
我将
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY
和 CLERK_SECRET_KEY
添加到 Azure 环境变量中,如下所示。
按照上述步骤操作后,我成功在Azure中运行了该应用程序。
Azure Web 应用程序输出: