Why must a Node.js app listen on 0.0.0.0 inside a container?
IntermediateConceptNode.js builds (npm, package.json)
Answer
Short answer
localhost / 127.0.0.1 means "only accept connections from inside this same container". Docker port mapping and Kubernetes Services send traffic to the container's network interface, not its loopback, so an app listening on localhost is unreachable. Listening on 0.0.0.0 means "accept connections on every network interface".
Example
app.listen(3000, "127.0.0.1"); // ❌ works with curl inside the container, unreachable from outside
app.listen(3000, "0.0.0.0"); // ✅ reachable via docker -p and Kubernetes Services
How you'd spot it
docker run -p 3000:3000 app→ browser shows "connection reset".- Inside the container:
ss -tlpnshows127.0.0.1:3000instead of0.0.0.0:3000or*:3000. - Kubernetes readiness probe fails with "connection refused".
The same applies to every stack: Flask (--host 0.0.0.0), Spring (server.address), .NET (ASPNETCORE_URLS=http://+:8080).
What to say in the interview
"Inside a container, localhost is the container's own loopback, so traffic from Docker's port mapping or a Kubernetes Service never reaches it. The app must bind to 0.0.0.0. I check with ss -tlpn inside the container."
More on Node.js builds (npm, package.json)
What is package.json and what does it contain?BeginnerHow do you build and run a Node.js application?BeginnerWhat is the difference between npm install and npm ci?BeginnerWhy must package-lock.json be committed to Git?BeginnerWhat is the difference between dependencies and devDependencies?BeginnerWhat do the ^ and ~ symbols mean in package.json versions?Beginner