docker-compose:MongoDB连接被拒绝

时间:2018-02-03 22:51:45

标签: node.js mongodb docker docker-compose

我是Docker的新手,我正在尝试在容器中设置现有项目。该项目是一个依赖于MongoDB的Node应用程序(通过Keystone JS运行)。

我的docker-compose.yml文件包含:

version: "2"

services:

  mongodb:
    image: mongo
    volumes:
      - /data/db:/data/db
    ports:
      - "27017:27017"
    command: mongod --bind_ip_all

web:
  build: .
  ports:
    - "3000:3000"
  links:
    - mongodb
  environment:
    - MONGO_URL=mongodb://mongodb/site

应用尝试使用MONGO_URL环境变量连接到数据库,但是出现以下错误:

web_1      | Mongoose connection "error" event fired with:
web_1      | { MongoError: failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017]
web_1      |     at Pool.<anonymous> (/opt/mainsite/node_modules/mongodb-core/lib/topologies/server.js:327:35)
web_1      |     at Pool.emit (events.js:160:13)
web_1      |     at Connection.<anonymous> (/opt/mainsite/node_modules/mongodb-core/lib/connection/pool.js:274:12)
web_1      |     at Object.onceWrapper (events.js:255:19)
web_1      |     at Connection.emit (events.js:160:13)
web_1      |     at Socket.<anonymous> (/opt/mainsite/node_modules/mongodb-core/lib/connection/connection.js:177:49)
web_1      |     at Object.onceWrapper (events.js:255:19)
web_1      |     at Socket.emit (events.js:160:13)
web_1      |     at emitErrorNT (internal/streams/destroy.js:64:8)
web_1      |     at process._tickCallback (internal/process/next_tick.js:152:19)
web_1      |   name: 'MongoError',
web_1      |   message: 'failed to connect to server [mongodb:27017] on first connect [MongoError: getaddrinfo ENOTFOUND mongodb mongodb:27017]' }
web_1      | Error: KeystoneJS (site) failed to start - Check that you are running `mongod` in a separate process.
web_1      |     at NativeConnection.<anonymous> (/opt/mainsite/node_modules/keystone/lib/core/openDatabaseConnection.js:59:10)
web_1      |     at NativeConnection.emit (events.js:160:13)
web_1      |     at Immediate.<anonymous> (/opt/mainsite/node_modules/keystone/node_modules/mongoose/lib/connection.js:317:19)
web_1      |     at runCallback (timers.js:756:18)
web_1      |     at tryOnImmediate (timers.js:717:5)
web_1      |     at processImmediate [as _immediateCallback] (timers.js:697:5)
web_1      | npm ERR! code ELIFECYCLE
web_1      | npm ERR! errno 1
web_1      | npm ERR! new_site@0.0.0 start: `node keystone`
web_1      | npm ERR! Exit status 1
web_1      | npm ERR! 
web_1      | npm ERR! Failed at the new_site@0.0.0 start script.
web_1      | npm ERR! This is probably not a problem with npm. There is likely additional logging output above.
web_1      | 
web_1      | npm ERR! A complete log of this run can be found in:
web_1      | npm ERR!     /root/.npm/_logs/2018-02-03T22_47_26_963Z-debug.log
new_web_1 exited with code 1

ENOTFOUND返回使得容器之间的网桥似乎有些不对劲,但我在网上找到的任何内容都无法解决此问题。

1 个答案:

答案 0 :(得分:0)

以下是您可能会执行的一些检查:

  • 主机名mongodb是否可以从您正在运行应用的docker中解析?您可以使用telnet来验证
  • mongodb在mongodb docker中运行吗?
  • mongodb是否只能在localhost接口上监听?