简体   繁体   English

ECONNREFUSED Docker 容器内部; 访问远程 API

[英]ECONNREFUSED Inside of Docker Container; Accessing Remote API

Recently, I've been building a nodejs bot to run games on a phpbb forum.最近,我一直在构建一个 nodejs 机器人来在 phpbb 论坛上运行游戏。 I have the bot running locally on my machine (logging in, posting, scraping threads, ect.), so, naturally, I've started to dockerize it.我让机器人在我的机器上本地运行(登录、发布、抓取线程等),所以很自然地,我已经开始对它进行 docker 化。

However, even when running through the login workflow from inside of my docker container, I'm receiving ECONNREFUSED errors from inside of the container when trying to hit the forum's api.但是,即使在我的 docker 容器内部运行登录工作流时,我在尝试访问论坛的 api 时也会从容器内部收到ECONNREFUSED错误。 There's no official docs for the aforementioned api, so a lot of this has been self-investigation.上述 api 没有官方文档,所以很多都是自我调查。

I'm running node v14.4.0 with axios v0.19.2 + toughcookie v4.0.0 for requests.我正在运行带有 axios v0.19.2 + strongcookie v4.0.0 的节点 v14.4.0 来处理请求。 I'm able to successfully curl the endpoint with the same payload from inside of the container, so I suspect there's something that's going over my head with axios/node.js.我能够从容器内部成功地使用相同的有效负载 curl 端点,所以我怀疑 axios/node.js 有什么东西让我头疼。 I've tried to look at other issues with docker and ECONNREFUSED, but most of them on stackoverflow relate to inter-container communication instead of issues with external apis/access.我试图查看 docker 和 ECONNREFUSED 的其他问题,但在 stackoverflow 上的大多数问题都与容器间通信有关,而不是与外部 apis/访问有关。

The container is running as a root user, and I don't have any sort of proxies or wonky networking set up.该容器以 root 用户身份运行,我没有设置任何类型的代理或不稳定的网络。

Does anyone have any advice or inklings?有没有人有任何建议或暗示? My docker-compose is pretty bare bones, but I've included it below for reference.我的 docker-compose 非常简单,但我已将其包含在下面以供参考。

version: '3.2'
services:
  bot:
    build: .
    env_file:
      - .env
    ports:
      - '80:80'

Any tips or theories would be much appreciated;任何提示或理论将不胜感激; I've hit the end of my list!我已经完成了我的清单!

Cheers!干杯!

I assume you try to reach the host from the container.我假设您尝试从容器中访问主机。 A simple way of accomplishing this is to use host networking instead of container networking.实现这一点的一种简单方法是使用主机网络而不是容器网络。 Try尝试

version: '3.2'
services:
  network_mode: host
  bot:
    build: .
    env_file:
      - .env

A better way is probably to put the rest of your application in container as well and use the service discovery in Compose, eg更好的方法可能是将应用程序的 rest 也放入容器中,并在 Compose 中使用服务发现,例如

version: '3.2'
services:
  bot:
    build: .
    env_file:
      - .env
  webapp:
    ...

Then you can reach the PHP app by connecting to the host name "webapp" in the above example.然后,您可以通过连接到上例中的主机名“webapp”来访问 PHP 应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM