Skip to content

Commit

Permalink
set hostname to 0.0.0.0 (#54342)
Browse files Browse the repository at this point in the history
When I try to deploy to Google Cloud Run it fails after some
investigation I saw this line in

.next/standalone/server.js
```javascript
const hostname = process.env.HOSTNAME || 'localhost'
``` 
This some how make this log when i run docker 
```shell
- ready started server on 172.17.0.2:3000, url: http://172.17.0.2:3000
``` 
I don't know why it's logging this address even if the server running on
localhost.

So this my fix
Set hostname to 0.0.0.0 to avoid deployment failing on Google cloud run.
  • Loading branch information
m4salah committed Aug 21, 2023
1 parent 3bce82e commit b7eb6d4
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions examples/with-docker/Dockerfile
Expand Up @@ -55,5 +55,7 @@ USER nextjs
EXPOSE 3000

ENV PORT 3000
# set hostname to localhost
ENV HOSTNAME "0.0.0.0"

CMD ["node", "server.js"]

4 comments on commit b7eb6d4

@mostlycloudysky
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did this fix work for you to force the app to run on localhost ?

@mareksuscak
Copy link

@mareksuscak mareksuscak commented on b7eb6d4 Sep 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just came here to say that something's recently changed (either Docker, NextJS or recent revisions of Node 18) because this started happening to us as well. After some dependency updates, Cloud Run's startup probe would keep failing and the service would be unavailable until we overrode the HOSTNAME variable. Cloud Run requires apps to listen on 0.0.0.0 because 127.0.0.1 refers to the loopback interface which is not available from outside the container. I believe that Docker sets the HOSTNAME variable to match that. I noticed in the logs that Next used to listen on http://localhost:$PORT according to the logs and then at some point that changed to http://127.0.0.1:$PORT which is when I found this thread after some poking around.

@isaacbatst
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I was trying localhost, but only this fixed. Proposing to change here as well.

@almightychang
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went in to same problem in Docker Swarm cluster with Traefik proxy. Fixed issue based on this commit. Thanks.

Please sign in to comment.