fix: bind Next.js standalone server to all interfaces for multi-network Docker setups#1104
Open
octo-patch wants to merge 1 commit intoItzCrazyKns:masterfrom
Open
Conversation
…er setups (fixes ItzCrazyKns#1102) When a container is attached to multiple Docker networks, the Next.js standalone server (server.js) defaults to binding on 'localhost' if HOSTNAME is not explicitly set. This causes the server to be unreachable when the container has multiple network interfaces, since traffic from the additional networks cannot reach localhost. Setting HOSTNAME=0.0.0.0 ensures the server listens on all network interfaces, resolving connectivity issues in multi-network Docker deployments.
|
If this acts like adding a manual / default environment variable |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1102
Problem
When a container is attached to multiple Docker networks, the Next.js standalone server (
server.js) defaults to binding onlocalhost(127.0.0.1) ifHOSTNAMEis not explicitly set. This causes the server to be unreachable when the container has multiple network interfaces, since incoming traffic routed through additional networks cannot reachlocalhost.This explains why the workaround of joining the extra network post-launch works — the container starts normally on a single network (with Docker's port proxy forwarding to 127.0.0.1:3000), and the extra network attach doesn't interfere with an already-established port binding.
Solution
Add
ENV HOSTNAME=0.0.0.0to bothDockerfileandDockerfile.slim. This sets theHOSTNAMEenvironment variable that the Next.js standaloneserver.jsreads at startup:With
HOSTNAME=0.0.0.0, the server listens on all network interfaces, making it reachable regardless of how many Docker networks the container is attached to.Testing
Summary by cubic
Bind the Next.js standalone server to 0.0.0.0 so containers attached to multiple Docker networks are reachable. Fixes #1102 by setting
HOSTNAME=0.0.0.0inDockerfileandDockerfile.slim, soserver.jslistens on all interfaces instead oflocalhost.Written for commit 0a17400. Summary will update on new commits.