ECONNREFUSED nodeJS 与 docker 容器内的 express [英] ECONNREFUSED nodeJS with express inside docker container

查看:17
本文介绍了ECONNREFUSED nodeJS 与 docker 容器内的 express的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个在 docker 容器中运行的 nodejs 应用程序...

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

由于app会调用第三方api,所以需要request模块.每次调用请求时,我都会收到以下错误...

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 }

我知道可能是因为 localhost 条目,但我该如何更改?

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

推荐答案

您是否将第三方 API 托管在本地主机上的 docker 容器中?如果是,那么您需要确保第三方 api docker 容器共享同一网络.请参阅 https://docs.docker.com/engine/userguide/networking/.我在尝试访问 rest 端点时遇到了同样的问题,我认为这将通过本地主机解决,因为它在我的容器内的 docker 上运行.以下是帮助我解决问题的一系列步骤:

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. 执行 docker network ls 以查看您有多少个网桥驱动程序.就我而言,我有 2 个容器,其中使用不同的容器
  2. 如果您有多个网桥驱动程序,请确保您启动的容器将使用相同的网桥网络相互通信 docker run -d -t --network networkname --name 容器名称
  3. 运行docker network inspect networkname.您将看到带有容器列表的网络详细信息.每个容器都有与之关联的 IPv4Address.使用这些地址的值代替 localhost 或 127.0.0.1 进行通信
  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

这篇关于ECONNREFUSED nodeJS 与 docker 容器内的 express的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆