
Docker said 'Connection refused'. Localhost worked fine. The problem was one line.
Last week I containerized an app that was running fine locally. Deployed to Docker. Got connection refused errors immediately. Checked the port mapping. Correct. Checked the environment variables. Correct. Restarted the container probably 15 times. Logged into the container to debug. Ran curl localhost:3000 from inside. Worked perfectly. Ran the same curl from my host machine. Connection refused. The actual problem The app was binding to 127.0.0.1:3000 . Docker containers need to bind to 0.0.0.0 to accept connections from outside the container. Localhost only accepts connections from inside the container itself. Changed this: const express = require ( ' express ' ); const app = express (); // This only works inside the container app . listen ( 3000 , ' 127.0.0.1 ' , () => { console . log ( ' Server running on port 3000 ' ); }); To this: const express = require ( ' express ' ); const app = express (); // This accepts external connections app . listen ( 3000 , ' 0.0.0.0 ' , () => { conso
Continue reading on Dev.to Webdev
Opens in a new tab




