Reputation: 45
I try to connect from a node.js backend to a different server running on localhost. Every server is running in a docker container. When I execute the http request I receive 'Error: connect ECONNREFUSED 127.0.0.1:80' or 'Error: connect ECONNREFUSED Error: connect ECONNREFUSED 127.0.0.1:4200' when connection to anything on localhost. My code looks the following:
const http = require('http');
const xy = http.get('http://localhost/path', (r) =>{
console.log(r);
});
xy.on('error', e => {
console.log(e);
});
const xz = http.get('http://localhost:4200', (r) =>{
console.log(r);
});
xz.on('error', e => {
console.log(e);
});
I do not understand, why it does not work. The same request in the browser does work. Connecting to different URLs like google.com does also work. Here is the output of netstat -na:
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN
tcp 0 0 127.0.0.1:631 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:5432 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:443 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:4222 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:4200 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:9000 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
Does anyone have an idea?
Regards
Upvotes: 2
Views: 5701
Reputation: 2467
localhost
inside a container refers to the container itself, not your laptop/server. So the other service is not running there.
If you want to connect from a container to another container you should use the name of the container instead localhost.
Also, containers need to be inside the same network
Upvotes: 3