连接拒绝:无法从Nodejs Docker容器访问RabbitMQ Docker容器
我在连接运行nodejs的Docker容器上的另一个Docker容器运行RabbitMQ时很难。
我谷歌搜索并环顾四周,但没有看到任何专门针对我遇到的问题。我不确定这是我缺少的东西还是其他。
要从nodejs连接,首先我尝试使用 ampqlib 库。这将使我从nodejs拒绝了连接。因此,我尝试寻找其他替代方案,并找到 rascal 。它是围绕ampqlib
的包装器。我看到两者都相同的错误:
Error: connect ECONNREFUSED 192.XXX.XXX.X:5672
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.XXX.XXX.X',
port: 5672,
broker: Symbol()
}
用于连接的NODEJS的连接字符串:
amqp:// $ {process.env.rabbitmq_default_user}:$ {process. env.rabbitmq_default_pass.rabbitmq_default_pass}美元
/代码>下面。
这些是从我检查的.env
文件中拉入的,并且Nodejs应用程序可以读取它们。
到目前为止,我已经测试过的事情:
- 我可以看到RabbitMQ容器正在运行,并准备在端口
5672
上
2022-05-27 15:16:06.422648+00:00 [info] <0.680.0> Ready to start client connection listeners
2022-05-27 15:16:06.429403+00:00 [info] <0.825.0> started TCP listener on [::]:5672
completed with 4 plugins.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> Server startup complete; 4 plugins started.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_prometheus
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_management
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_web_dispatch
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_management_agent
- 进行连接:
我可以通过port
15672 使用通过ENV变量设置的自定义用户/通过I。我必须配置nginx反向代理,但起作用。
docker-compose
相关作品看起来像这样:
#################
### rabbit mq ###
#################
rabbitmq:
hostname: rabbitmq
container_name: rabbitmq
image: rabbitmq:management
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
RABBITMQ_DEFAULT_HOST: ${RABBITMQ_DEFAULT_HOST}
ports:
# AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
######################
### nodejs-client ####
######################
api-client:
container_name: api-client
restart: unless-stopped
build: ./packages/market/api-client
env_file:
- ./packages/market/api-client/.env
depends_on:
- rabbitmq
- api
links:
- rabbitmq
- api
- 确保用户名/密码没有任何奇怪的字符可能会破坏字符串,因此只需使用test:test:test:test:bot test:bot test:nock and nock and nock of coss。
如果有人以前遇到过这个问题或有任何想法或建议,那么我很感激我很感激!谢谢,
- 我没有配置除默认网络以外的任何码头网络。当我做
Docker网络检查App_default
时,我可以看到Rabbitmq
和api-client
该网络列出的容器。
I am having trouble connecting a Docker container running NodeJS to another docker container running RabbitMQ.
I googled and looked around but did not see anything specifically for issue I am running into. I am not sure if this something silly I am missing or something else.
To connect from NodeJS, first I tried just using the ampqlib library. And that would give me a connection refused from NodeJS. So then I tried looking for other alternatives and found Rascal. It is a wrapper around ampqlib
. I see same error for both:
Error: connect ECONNREFUSED 192.XXX.XXX.X:5672
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1195:16) {
errno: -111,
code: 'ECONNREFUSED',
syscall: 'connect',
address: '192.XXX.XXX.X',
port: 5672,
broker: Symbol()
}
The connection string from NodeJS used to connect:
amqp://${process.env.RABBITMQ_DEFAULT_USER}:${process.env.RABBITMQ_DEFAULT_PASS}@${process.env.RABBITMQ_DEFAULT_HOST}:${process.env.RABBITMQ_DEFAULT_PORT}
;`
The HOST
for this is RABBITMQ_DEFAULT_HOST=rabbitmq
as what I have set in the docker-compose
below.
These are pulled in from a .env
file that I checked and the NodeJS app can read them fine.
Things I have tested so far:
- I can see the rabbitmq container running and ready for connections on port
5672
:
2022-05-27 15:16:06.422648+00:00 [info] <0.680.0> Ready to start client connection listeners
2022-05-27 15:16:06.429403+00:00 [info] <0.825.0> started TCP listener on [::]:5672
completed with 4 plugins.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> Server startup complete; 4 plugins started.
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_prometheus
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_management
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_web_dispatch
2022-05-27 15:16:06.627770+00:00 [info] <0.680.0> * rabbitmq_management_agent
I can access the RabbitMQ management UI through web browser on port
15672
using the custom user/pass I set through env variables. I had to configure the NGINX reverse proxy but it worked.The
docker-compose
for relevant pieces looks like this:
#################
### rabbit mq ###
#################
rabbitmq:
hostname: rabbitmq
container_name: rabbitmq
image: rabbitmq:management
environment:
RABBITMQ_DEFAULT_USER: ${RABBITMQ_DEFAULT_USER}
RABBITMQ_DEFAULT_PASS: ${RABBITMQ_DEFAULT_PASS}
RABBITMQ_DEFAULT_HOST: ${RABBITMQ_DEFAULT_HOST}
ports:
# AMQP protocol port
- '5672:5672'
# HTTP management UI
- '15672:15672'
######################
### nodejs-client ####
######################
api-client:
container_name: api-client
restart: unless-stopped
build: ./packages/market/api-client
env_file:
- ./packages/market/api-client/.env
depends_on:
- rabbitmq
- api
links:
- rabbitmq
- api
- Making sure the username/password did not have any strange characters that could break the string, so just using things like test:test but still no luck.
If anyone has run into this before or has any ideas or suggestions I would greatly appreciate as I am stumped! Thank you
- I am not configuring any Docker network other than the default one. When I do
docker network inspect app_default
I can see therabbitmq
andapi-client
containers listed for this network.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我弄清楚了这个问题:我需要让Nodejs应用程序等待一些时间,然后再尝试连接到RabbitMQ容器。我将
settimeout
等待几分钟,并且能够成功连接。我不知道有更好的等待方式。我之前曾尝试过
Wait-for-it
,也许我会编写一个特定于我的设置的自定义bash脚本,以便在一段时间之后仅启动nodejs容器。I figured out the issue: I needed to have the NodeJS app wait some time before trying to connect to RabbitMQ container. I put a
setTimeout
to wait for a couple of minutes and it was able to successfully connect.I do not know of a better way to wait. I have tried
wait-for-it
before, maybe I will write a custom bash script that is specific to my setup to only start the NodeJS container after some time has passed.