英文:
Docker : impossible connection between MongoDB and another docker app
问题
我在使用Docker启动的MongoDB(端口:27017
)之间遇到了连接问题,但是当我尝试启动我的应用程序(需要使用数据库来启动它)时,我卡在了这个错误上:MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
。
我尝试了https://stackoverflow.com/questions/52417322/connecting-to-mongodb-docker-container-from-another-docker-container 上的解决方案,但仍然无法解决这个问题...
有人对这个问题有什么想法吗?
我尝试过使用在线数据库,但仍然出现错误(MongoServerError: bad auth : authentication failed
),因为我希望它在我的服务器上启动,我不想花太多时间在这个解决方案上(即使它能够工作,这也不是情况)。
我尝试使用mongodb://myip:27017
而不是mongodb://localhost:27017
,但仍然遇到问题。
英文:
I'm having an issue with the connection between my MongoDB that I launched with docker (port:27017
) but when I try to launch my app (the DB is needed for launching it), I am stuck with this error : MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
.
I tried the solution at https://stackoverflow.com/questions/52417322/connecting-to-mongodb-docker-container-from-another-docker-container but sill could not figure how to solve this issue...
Anyone have a thought about this issue ?
docker run chat-ui
-------------
__/\\\\\\\\\\\\\____/\\\\____________/\\\\____/\\\\\\\\\_____
_\/\\\/////////\\\_\/\\\\\\________/\\\\\\__/\\\///////\\\___
_\/\\\_______\/\\\_\/\\\//\\\____/\\\//\\\_\///______\//\\\__
_\/\\\\\\\\\\\\\/__\/\\\\///\\\/\\\/_\/\\\___________/\\\/___
_\/\\\/////////____\/\\\__\///\\\/___\/\\\________/\\\//_____
_\/\\\_____________\/\\\____\///_____\/\\\_____/\\\//________
_\/\\\_____________\/\\\_____________\/\\\___/\\\/___________
_\/\\\_____________\/\\\_____________\/\\\__/\\\\\\\\\\\\\\\_
_\///______________\///______________\///__\///////////////__
Runtime Edition
PM2 is a Production Process Manager for Node.js applications
with a built-in Load Balancer.
Start and Daemonize any application:
$ pm2 start app.js
Load Balance 4 instances of api.js:
$ pm2 start api.js -i 4
Monitor in production:
$ pm2 monitor
Make pm2 auto-boot at server restart:
$ pm2 startup
To go further checkout:
http://pm2.io/
-------------
pm2 launched in no-daemon mode (you can add DEBUG="*" env variable to get more messages)
2023-06-19T09:18:59: PM2 log: Launching in no daemon mode
2023-06-19T09:18:59: PM2 log: [PM2] Starting /app/build/index.js in cluster_mode (0 instance)
2023-06-19T09:18:59: PM2 log: App [index:0] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:0] online
2023-06-19T09:18:59: PM2 log: App [index:1] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:1] online
2023-06-19T09:18:59: PM2 log: App [index:2] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:2] online
2023-06-19T09:18:59: PM2 log: App [index:3] starting in -cluster mode-
2023-06-19T09:18:59: PM2 log: App [index:3] online
2023-06-19T09:18:59: PM2 log: [PM2] Done.
2023-06-19T09:18:59: PM2 log: ┌────┬──────────┬─────────────┬─────────┬─────────┬──────────┬────────┬──────┬───────────┬──────────┬──────────┬──────────┬──────────┐
│ id │ name │ namespace │ version │ mode │ pid │ uptime │ ↺ │ status │ cpu │ mem │ user │ watching │
├────┼──────────┼─────────────┼─────────┼─────────┼──────────┼────────┼──────┼───────────┼──────────┼──────────┼──────────┼──────────┤
│ 0 │ index │ default │ 0.3.0 │ cluster │ 19 │ 0s │ 0 │ online │ 0% │ 61.7mb │ root │ disabled │
│ 1 │ index │ default │ 0.3.0 │ cluster │ 26 │ 0s │ 0 │ online │ 0% │ 52.9mb │ root │ disabled │
│ 2 │ index │ default │ 0.3.0 │ cluster │ 33 │ 0s │ 0 │ online │ 0% │ 51.0mb │ root │ disabled │
│ 3 │ index │ default │ 0.3.0 │ cluster │ 44 │ 0s │ 0 │ online │ 0% │ 45.3mb │ root │ disabled │
└────┴──────────┴─────────────┴─────────┴─────────┴──────────┴────────┴──────┴───────────┴──────────┴──────────┴──────────┴──────────┘
2023-06-19T09:18:59: PM2 log: [--no-daemon] Continue to stream logs
2023-06-19T09:18:59: PM2 log: [--no-daemon] Exit on target PM2 exit pid=8
09:18:59 0|index | Listening on 0.0.0.0:3000
09:18:59 1|index | Listening on 0.0.0.0:3000
09:18:59 2|index | Listening on 0.0.0.0:3000
09:18:59 3|index | Listening on 0.0.0.0:3000
09:19:29 0|index | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 0|index | at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 0|index | at listOnTimeout (node:internal/timers:573:17)
09:19:29 0|index | at process.processTimers (node:internal/timers:514:7) {
09:19:29 0|index | reason: TopologyDescription {
09:19:29 0|index | type: 'Unknown',
09:19:29 0|index | servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 0|index | stale: false,
09:19:29 0|index | compatible: true,
09:19:29 0|index | heartbeatFrequencyMS: 10000,
09:19:29 0|index | localThresholdMS: 15,
09:19:29 0|index | setName: null,
09:19:29 0|index | maxElectionId: null,
09:19:29 0|index | maxSetVersion: null,
09:19:29 0|index | commonWireVersion: 0,
09:19:29 0|index | logicalSessionTimeoutMinutes: null
09:19:29 0|index | },
09:19:29 0|index | code: undefined,
09:19:29 0|index | [Symbol(errorLabels)]: Set(0) {}
09:19:29 0|index | }
09:19:29 1|index | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 1|index | at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 1|index | at listOnTimeout (node:internal/timers:573:17)
09:19:29 1|index | at process.processTimers (node:internal/timers:514:7) {
09:19:29 1|index | reason: TopologyDescription {
09:19:29 1|index | type: 'Unknown',
09:19:29 1|index | servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 1|index | stale: false,
09:19:29 1|index | compatible: true,
09:19:29 1|index | heartbeatFrequencyMS: 10000,
09:19:29 1|index | localThresholdMS: 15,
09:19:29 1|index | setName: null,
09:19:29 1|index | maxElectionId: null,
09:19:29 1|index | maxSetVersion: null,
09:19:29 1|index | commonWireVersion: 0,
09:19:29 1|index | logicalSessionTimeoutMinutes: null
09:19:29 1|index | },
09:19:29 1|index | code: undefined,
09:19:29 1|index | [Symbol(errorLabels)]: Set(0) {}
09:19:29 1|index | }
09:19:29 2|index | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 2|index | at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 2|index | at listOnTimeout (node:internal/timers:573:17)
09:19:29 2|index | at process.processTimers (node:internal/timers:514:7) {
09:19:29 2|index | reason: TopologyDescription {
09:19:29 2|index | type: 'Unknown',
09:19:29 2|index | servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 2|index | stale: false,
09:19:29 2|index | compatible: true,
09:19:29 2|index | heartbeatFrequencyMS: 10000,
09:19:29 2|index | localThresholdMS: 15,
09:19:29 2|index | setName: null,
09:19:29 2|index | maxElectionId: null,
09:19:29 2|index | maxSetVersion: null,
09:19:29 2|index | commonWireVersion: 0,
09:19:29 2|index | logicalSessionTimeoutMinutes: null
09:19:29 2|index | },
09:19:29 2|index | code: undefined,
09:19:29 2|index | [Symbol(errorLabels)]: Set(0) {}
09:19:29 2|index | }
09:19:29 3|index | MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
09:19:29 3|index | at Timeout._onTimeout (/app/node_modules/mongodb/lib/sdam/topology.js:277:38)
09:19:29 3|index | at listOnTimeout (node:internal/timers:573:17)
09:19:29 3|index | at process.processTimers (node:internal/timers:514:7) {
09:19:29 3|index | reason: TopologyDescription {
09:19:29 3|index | type: 'Unknown',
09:19:29 3|index | servers: Map(1) { 'localhost:27017' => [ServerDescription] },
09:19:29 3|index | stale: false,
09:19:29 3|index | compatible: true,
09:19:29 3|index | heartbeatFrequencyMS: 10000,
09:19:29 3|index | localThresholdMS: 15,
09:19:29 3|index | setName: null,
09:19:29 3|index | maxElectionId: null,
09:19:29 3|index | maxSetVersion: null,
09:19:29 3|index | commonWireVersion: 0,
09:19:29 3|index | logicalSessionTimeoutMinutes: null
09:19:29 3|index | },
09:19:29 3|index | code: undefined,
09:19:29 3|index | [Symbol(errorLabels)]: Set(0) {}
09:19:29 3|index | }
I tried using an online DB but I was still getting an error (MongoServerError: bad auth : authentication failed
) because I wanted it to launch on my server, I didn't wanted to spend too much time on this solution (even in the case it would be working, which was not the case).
I tried using mongodb://myip:27017
instead of mongodb://localhost:27017
yet, still having an issue.
答案1
得分: 1
当你在容器中使用localhost
时,它并不代表你的个人电脑,在容器的上下文中,localhost
表示容器本身。容器内部的服务在隔离的环境中运行。所以这就是为什么容器chat-ui
无法通过localhost:27017
连接到mongodb
容器的原因。
连接不同容器的方法:
- 你可以使用Docker网络。因为这允许容器在一个网络中相互通信。
- 或者你可以使用Docker Compose来部署多个容器。
- 还有另一种容器链接的方法,使用传统容器链接。但这不被推荐,因为它最终可能会被移除。
另外,参考以下链接:
- https://stackoverflow.com/questions/41093812/how-to-get-docker-containers-to-talk-to-each-other-while-running-on-my-local-hos
- https://stackoverflow.com/questions/41768157/how-to-link-containers-in-docker
希望这些信息能帮助你解决问题。
英文:
When you use localhost
in your container, It doesn't mean your PC, in the context of a container localhost
means the container itself. The services inside a container runs in an isolated environment.
So that's why the container chat-ui
unable to connect with the mongodb
container with localhost:27017
.
The way to connect different containers
- You can use Docker Networking. As this allows containers to communicate with each other in a network.
- OR you can deploy multiple containers by using Docker compose
- There is another approach for container linking with Legacy container links. But this is not recommended as it may eventually be removed
Also see these
- https://stackoverflow.com/questions/41093812/how-to-get-docker-containers-to-talk-to-each-other-while-running-on-my-local-hos
- https://stackoverflow.com/questions/41768157/how-to-link-containers-in-docker
Hope this will help you to resolve
答案2
得分: 0
你是否已将Mongo容器的27017端口暴露给主机?
尝试使用以下命令之一运行Mongo容器:
docker run -p 27017:27017 --name some-mongo -d mongo:tag
或者:
docker run --network="host" --name some-mongo -d mongo:tag
英文:
Have you exposed the mongo container's 27017 port to your host?
Try running the mongo container with either:
docker run -p 27017:27017 --name some-mongo -d mongo:tag
or:
docker run --network="host" --name some-mongo -d mongo:tag
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论