NodeJS / Express无法连接到docker中运行的mongodb

从nodejs / express应用程序连接到在docker容器中运行的mongodb实例时遇到问题。 nodejs应用程序正在我的本地主机Mac OS X上运行。我正在使用Docker Toolbox在我的Mac上运行docker,而不是boot2docker。

我已经启用端口转发通过-p arg到docker运行。

docker run -p 27017:27017 --name test -d mongo:2.6 60b7f3cf658bc75ceca2eae074e5f694ec622f7487b6a2f36fa02edd4af68357 

我可以看到它正在运行。

 docker ps CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 60b7f3cf658b mongo:2.6 "/entrypoint.sh mongo" 9 minutes ago Up 9 minutes 0.0.0.0:27017->27017/tcp test 

如果我运行docker日志,我看到它正在监听端口27017。

 docker logs test mongod --help for help and startup options 2015-10-03T15:42:25.833+0000 [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=60b7f3cf658b 2015-10-03T15:42:25.833+0000 [initandlisten] db version v2.6.11 2015-10-03T15:42:25.833+0000 [initandlisten] git version: d00c1735675c457f75a12d530bee85421f0c5548 2015-10-03T15:42:25.833+0000 [initandlisten] build info: Linux build4.ny.cbi.10gen.cc 2.6.32-431.3.1.el6.x86_64 #1 SMP Fri Jan 3 21:39:27 UTC 2014 x86_64 BOOST_LIB_VERSION=1_49 2015-10-03T15:42:25.833+0000 [initandlisten] allocator: tcmalloc 2015-10-03T15:42:25.833+0000 [initandlisten] options: {} 2015-10-03T15:42:25.835+0000 [initandlisten] journal dir=/data/db/journal 2015-10-03T15:42:25.835+0000 [initandlisten] recover : no journal files present, no recovery needed 2015-10-03T15:42:25.860+0000 [initandlisten] allocating new ns file /data/db/local.ns, filling with zeroes... 2015-10-03T15:42:25.891+0000 [FileAllocator] allocating new datafile /data/db/local.0, filling with zeroes... 2015-10-03T15:42:25.891+0000 [FileAllocator] creating directory /data/db/_tmp 2015-10-03T15:42:25.892+0000 [FileAllocator] done allocating datafile /data/db/local.0, size: 64MB, took 0 secs 2015-10-03T15:42:25.893+0000 [initandlisten] build index on: local.startup_log properties: { v: 1, key: { _id: 1 }, name: "_id_", ns: "local.startup_log" } 2015-10-03T15:42:25.893+0000 [initandlisten] added index to empty collection 2015-10-03T15:42:25.894+0000 [initandlisten] waiting for connections on port 27017 

当我尝试从express连接时,出现以下错误。

 my-app/node_modules/mongodb/lib/server.js:228 process.nextTick(function() { throw err; }) ^ Error: connect ECONNREFUSED at exports._errnoException (util.js:746:11) at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19) 

现在,在stackoverflow上还有其他的post引用了这个确切的东西。 他们都说“通过-p arg启用端口转发”,我这样做。 但它仍然不会让我连接。

我正在使用正确的端口和主机虚拟机IP – 我可以通过常规的mongo shell工具从我的Mac连接到mongodb容器。

还有一些奇怪的事情…

  • 我可以通过mongo shell从我的Mac连接到容器mongodb。 运行docker日志testing时显示连接。
  • 我在日志中看到来自我的nodejs应用程序的连接,但是立即断开连接。

 2015-10-03T15:53:25.938+0000 [clientcursormon] connections:0 2015-10-03T15:54:08.843+0000 [initandlisten] connection accepted from 192.168.99.1:54804 #1 (1 connection now open) 2015-10-03T15:54:08.855+0000 [conn1] end connection 192.168.99.1:54804 (0 connections now open) 

也可以看看:

  • 无法连接到docker容器中运行的mongoDB
  • Node / Express应用程序无法连接到docker mongodb

我要回答我自己的问题,但不接受它。 所以事实certificate,这个错误是由另外一段正在尝试连接到mongodb://127.0.0.1:27017的掩埋nodejs代码造成的。 这个mongodb实例不存在,所以抛出错误。

我集中了MongoDBconfiguration,一切都很好。