Docker 主机名未在 next.js prod 中解析,但在开发模式下工作(错误:getaddrinfo ENOTFOUND)

mdw*_*326 8 node.js docker docker-compose next.js

我正在 docker 容器中运行 next.js react 应用程序。它由其他几个包含组成:一个运行 Ghost(我正在使用 API),一个运行 mysql,一个运行 NGINX。我让一切都在开发模式下运行。

使用next dev. 但是当我通过执行next build和运行它时next start,我开始看到错误,例如Error: getaddrinfo ENOTFOUND ghost-api当我尝试向我的 Ghost API 容器发出服务器端 HTTP 请求时。我不完全确定问题是什么,但似乎 Node 在构建后如何发出请求存在一些问题。我一直在挖掘很多 Docker/Node 问题,试图解决这个问题,但没有任何运气。

整个项目可以在这里找到:https : //github.com/MichaelWashburnJr/react-cms

Cha*_*mar 1

问题可能存在于您正在使用的环境变量中。在 getGhostApi 和 getGhostApiKey 函数中,您都使用环境变量。

在 NextJs 中,您必须指定 next.config.js,在其中定义所需的变量

前任。next.config.js

module.exports = {
  serverRuntimeConfig: {
    // Will only be available on the server side
    mySecret: 'secret',
    secondSecret: process.env.SECOND_SECRET, // Pass through env variables
  },
  publicRuntimeConfig: {
    // Will be available on both server and client
    staticFolder: '/static',
  },
}
Run Code Online (Sandbox Code Playgroud)

您还可以参考下一个文档以获取相同的信息。 https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration