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 -tlpn shows 127.0.0.1:3000 instead of 0.0.0.0:3000 or *: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."

All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Node.js apps: npm & package.json

More on Node.js builds (npm, package.json)