You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker部署Next.js调用.NET Core API遇ECONNREFUSED错误求助

问题:Docker中Next.js的getServerSideProps调用API失败(ECONNREFUSED)

我在Docker中运行Next.js应用时,通过getServerSideProps调用API出现连接拒绝错误,但客户端调用API(包括鉴权和公开请求)均正常。本地直接运行Next.js应用时一切正常。

错误信息

TypeError: fetch failed
   at node:internal/deps/undici/undici:13178:13
   at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
   at async m (/app/.next/server/chunks/961.js:3:716)
   at async b (/app/.next/server/pages/user/administration.js:51:1030)
   at async e3 (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/compiled/next-server/pages.runtime.prod.js:31:594)
   at async doRender (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/server/base-server.js:1425:30)
   at async cacheEntry.responseCache.get.routeKind (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/server/base-server.js:1599:28)
   at async NextNodeServer.renderToResponseWithComponentsImpl (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/server/base-server.js:1507:28)
   at async NextNodeServer.renderPageComponent (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/server/base-server.js:1924:24)
   at async NextNodeServer.renderToResponseImpl (/app/node_modules/.pnpm/next@14.2.2_@babel+core@7.24.4_react-dom@18.2.0_react@18.2.0_sass@1.75.0/node_modules/next/dist/server/base-server.js:1962:32) {
 [cause]: AggregateError [ECONNREFUSED]:
     at internalConnectMultiple (node:net:1118:18)
     at afterConnectMultiple (node:net:1685:7)
     at TCPConnectWrap.callbackTrampoline (node:internal/async_hooks:130:17) {
   code: 'ECONNREFUSED',
   [errors]: [ [Error], [Error] ]
}

环境说明

  • API基于.NET Core构建,运行在localhost:5068(未部署在Docker中)
  • Next.js应用通过Docker运行在localhost:3050

配置文件

Dockerfile

FROM node:22.4-alpine AS base

FROM base AS deps
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json pnpm-lock.yaml* ./
RUN corepack enable pnpm && pnpm i --frozen-lockfile

FROM base AS builder
ENV NEXT_PRIVATE_STANDALONE true
ENV NEXT_PUBLIC_BACKEND_ROOT_URI http://localhost:5068
ENV NEXT_PUBLIC_FRONTEND_URI http://localhost:3050
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN corepack enable pnpm && pnpm run build

FROM base AS runner
WORKDIR /app
ENV NODE_ENV production
ENV NEXT_TELEMETRY_DISABLED 1
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs
COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
RUN mkdir .next
RUN chown nextjs:nodejs .next
COPY --from=builder --chown=nextjs:nodejs /app/.next/standalone ./
COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
USER nextjs
EXPOSE 3050
ENV PORT 3050
CMD HOSTNAME="0.0.0.0" node server.js

Docker Compose

services:
  sweetnotes.fe:
    image: myimage
    container_name: mycontainer
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - '3050:3050'

解决方案

核心原因

Docker容器内的localhost指向容器自身,而非宿主机。getServerSideProps在容器内的Node.js环境执行,调用http://localhost:5068会尝试连接容器内部的5068端口,但你的API运行在宿主机,因此出现连接拒绝。客户端调用正常是因为浏览器的localhost指向宿主机。

方法1:使用宿主机IP地址修改API地址

将Next.js的服务端API地址替换为宿主机的实际IP(比如192.168.1.100,可通过ipconfig/ifconfig查看)。

方式A:修改Dockerfile

在builder阶段更新环境变量:

ENV NEXT_PUBLIC_BACKEND_ROOT_URI http://你的宿主机IP:5068

方式B:修改Docker Compose(更灵活)

在服务配置中添加环境变量:

services:
  sweetnotes.fe:
    # ... 原有配置
    environment:
      - NEXT_PUBLIC_BACKEND_ROOT_URI=http://你的宿主机IP:5068

方法2:使用Docker宿主机网络模式(Linux专属)

让容器直接共享宿主机网络,此时容器内的localhost会指向宿主机:

services:
  sweetnotes.fe:
    # ... 原有配置
    network_mode: host

注意:此模式下ports配置会失效,直接访问宿主机3050端口即可。

方法3:使用Docker网关地址

Docker默认网关IP通常为172.17.0.1(可通过docker inspect bridge确认),将API地址改为该网关IP:

ENV NEXT_PUBLIC_BACKEND_ROOT_URI http://172.17.0.1:5068

同时确保.NET Core API监听所有网络接口,修改Program.cs:

var app = builder.Build();
app.Run("http://0.0.0.0:5068");

注意事项

  • 服务端(getServerSideProps)和客户端的API地址可区分:客户端用localhost:5068没问题,服务端必须用能访问宿主机的地址。
  • 确保宿主机防火墙允许容器所在网络访问5068端口。

内容的提问来源于stack exchange,提问作者esausilva

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 01:32:02