Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

152
Views
Error: connect ECONNREFUSED 127.0.0.1 when connection to anything on localhost

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

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!