Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Docker Hostnames not resolving in next.js prod but working in dev mode (Error: getaddrinfo ENOTFOUND)

I'm running a next.js react app in a docker container. It's being composed with several other contains: one running Ghost (I'm using the API), one running mysql, and one running NGINX. I've got everything running in development mode.

It works perfectly when run using next dev. But when I run it by doing next build and next start, I start seeing errors like Error: getaddrinfo ENOTFOUND ghost-api when I try to make server-side HTTP requests to my Ghost API container. I'm not entirely sure what the issue is but it seems like there's some issue with how Node is making requests after being built. I've been digging through a lot of Docker/Node questions trying to figure this one out but haven't had any luck.

The entire project can be found here: https://github.com/MichaelWashburnJr/react-cms

like image 217
mdw7326 Avatar asked Jun 14 '20 19:06

mdw7326


People also ask

How do I fix Getaddrinfo Enotfound error?

The getaddrinfo error can be solved by passing the entire URL instead of the options to the http. get() function. You can kick away the getaddrinfo error by adding the missing localhost line in the hosts file while using localhost.

What does Getaddrinfo Enotfound mean?

getaddrinfo ENOTFOUND means client was not able to connect to given address.


1 Answers

The problem may exist in the environment variable that you are using. In both getGhostApi and getGhostApiKey function, you are using the environment variable.

In NextJs you'll have to specify a next.config.js in which you define the variables that you need for

Ex. 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',
  },
}

You can also refer to the next documentation for the same. https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration

like image 134
Chandan Kumar Avatar answered Sep 27 '22 20:09

Chandan Kumar