25

I'm building up a nodejs app which is running in the docker container...

This is the command I used to run the container...

sudo docker run -it --rm -p 3000:6001 --name xxx-running xxx

Execute above command line, got following output..

    Running on Locally
    AppEnv {
      isLocal: true,
      app: {},
      services: {},
      name: 'xxx',
      port: 6001,
      bind: '0.0.0.0',
      urls: [ 'http://localhost:6001' ],
      url: 'http://localhost:6001' }
   App started on port http://localhost:6001

Since the app will call third party api, so the request module is required. Each time when a request is invoked, I got following error...

{ [Error: connect ECONNREFUSED 192.155.253.83:443]
 code: 'ECONNREFUSED',
 errno: 'ECONNREFUSED',
 syscall: 'connect',
 address: '192.155.253.83',
 port: 443 }

I know maybe it's because of the localhost entry, but how can I change this?

Yinjun Zhang
  • 275
  • 1
  • 3
  • 8

2 Answers2

30

Are you hosting your third party api inside a docker container on localhost? If yes then you need to make sure that third-party api docker container is sharing same network. See https://docs.docker.com/engine/userguide/networking/. I had same problem trying to access rest endpoint which i assumed will be resolved with local host since it is running on my docker inside container. Here is a sequence of steps which helped me resolved issue:

  1. Execute docker network ls to see how many bridge drivers you have. In my case i had 2 and containers where using different ones
  2. If you have multiple bridge drivers, make sure that you starting your containers which will be talking with each other using same bridge network docker run -d -t --network networkname --name containername
  3. Run docker network inspect networkname. You will see details of network with list of containers. Each container will have IPv4Address associated with it. Use value of these address to communicate instead of localhost or 127.0.0.1
Ziyaddin Sadigov
  • 8,893
  • 13
  • 34
  • 41
George Trifonov
  • 1,971
  • 17
  • 20
  • I'm facing the same issue and tried that already but still see the same error. I have put on here https://stackoverflow.com/questions/60534429/access-docker-container-from-another-docker-container – Sheraz Ali Mar 04 '20 at 21:41
-1

When your app access localhost is accessing to the localhost of the container network, for localhost of your environment try to add the --network host on your run command like this:

-sudo docker run --network host -it --rm -p 3000:6001 --name xxx-running xxx
Peppermintology
  • 9,343
  • 3
  • 27
  • 51