简体   繁体   English

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

[英]NodeJS / Express cannot connect to mongodb running in docker

I'm having a problem connecting from a nodejs/express app to a mongodb instance running in a docker container. 我在从nodejs / express应用程序连接到在docker容器中运行的mongodb实例时遇到问题。 The nodejs app is running on my local host, Mac OS X. I am using the Docker Toolbox to run docker on my Mac, not boot2docker. nodejs应用程序正在我的本地主机Mac OS X上运行。我正在使用Docker Toolbox在我的Mac上运行docker,而不是boot2docker。

I have enabled port forwarding via the -p arg to docker run. 我已经通过-p arg启用了端口转发到docker run。

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

I can see it running. 我可以看到它在运行。

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

If I run docker logs, I see that it is listening on port 27017. 如果我运行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

When I attempt to connect from express, I get the following error. 当我尝试从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)

Now, there are other posts on stackoverflow which reference this exact thing. 现在,stackoverflow上有其他帖子引用了这个确切的东西。 They all say "enable port forwarding via the -p arg," which I did. 他们都说“通过-p arg启用端口转发”,我这样做了。 But it still won't let me connect. 但它仍然不会让我联系。

I'm using the correct port and host VM IP - I can connect from my Mac to the mongodb container via the regular mongo shell tool. 我正在使用正确的端口和主机VM IP - 我可以通过常规的mongo shell工具从我的Mac连接到mongodb容器。

There are some strange things also... 还有一些奇怪的事情......

  • I can connect from my Mac via the mongo shell to the container mongodb just fine. 我可以通过mongo shell从我的Mac连接到容器mongodb就好了。 The connection shows up when I run docker logs test. 当我运行docker logs test时,连接会显示出来。
  • I see the connection from my nodejs app in the logs as well, but then an immediate disconnect. 我也看到了日志中我的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)

See also: 也可以看看:

I'm going to answer my own question but not accept it. 我将回答我自己的问题,但不接受它。 So it turns out the error was caused by another section of buried nodejs code which was attempting to connect to mongodb://127.0.0.1:27017. 所以事实证明错误是由另一部分埋藏的nodejs代码引起的,该代码试图连接到mongodb://127.0.0.1:27017。 This mongodb instance does not exist, so it was throwing the error. 这个mongodb实例不存在,所以它抛出错误。

I centralized the mongodb config and everything is golden. 我集中了mongodb配置,一切都是金色的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM