You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Docker环境下MongoDB与应用容器连接失败求助

问题:Docker部署的Chat-UI应用无法连接MongoDB数据库

我用Docker启动了MongoDB(端口映射到27017),但启动依赖该数据库的chat-ui应用容器时,遇到连接错误:
MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017

我尝试过以下方案,但都没解决问题:

  • 按照跨容器连接的方案,把数据库连接地址从mongodb://localhost:27017改成mongodb://myip:27017
  • 尝试切换到在线MongoDB数据库,却出现认证错误:MongoServerError: bad auth : authentication failed

以下是chat-ui应用的启动日志:

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  | }

内容的提问来源于stack exchange,提问作者SCM

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 15:22:34