The Container Port Binding Mistake That Breaks Almost Every First Deploy
You deploy your app. The build succeeds. The logs show the server starting. You click the URL your deployment platform gave you and get a connection error, a 502, or nothing at all. This is one of the most common first deployment failures, and the cause is almost always the same: the app is binding to the wrong address, or listening on the wrong port, or both. What port binding actually means When a server app starts, it listens for incoming connections on a network address. That address has two parts: the IP address it listens on, and the port number. The IP address determines which network interfaces the application accepts connections from. localhost (which resolves to 127.0.0.1 ) means the app only accepts connections from the same machine. 0.0.0.0 means the app accepts connections from any network interface, including external ones. During local development, localhost is fine. Everything is on the same machine. Your browser and your server are both on your laptop. When you deploy to a server, the platform's load balancer is not on the same machine as your app. It is trying to connect from outside. An app bound to localhost is invisible to it. The port problem Deployment platforms often assign ports dynamically. They tell your app which port to use through an environment variable, almost always called PORT . Your app needs to read this variable and bind to that port. If your app ignores PORT and hardcodes a port number, it starts on a port the platform is not watching. The platform tries to connect on its assigned port, gets nothing, and marks the deployment as failed. // This will fail on most platforms app . listen ( 3000 ) // This is correct app . listen ( process . env . PORT || 3000 ) The || 3000 fallback makes the app work both locally (where PORT is not set) and in production (where the platform sets it). What AI tools generate AI tools often hardcode both the address and the port. The generated code looks like this: app . listen ( 3000 , ' localhost ' ,