3

I'm having a problem connecting from a nodejs/express app to a mongodb instance running in a docker container. 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.

I have enabled port forwarding via the -p arg to 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 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.

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. They all say "enable port forwarding via the -p arg," which I did. 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.

There are some strange things also...

  • I can connect from my Mac via the mongo shell to the container mongodb just fine. The connection shows up when I run docker logs test.
  • I see the connection from my nodejs app in the logs as well, but then an immediate disconnect.


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:

Community
  • 1
  • 1
Mike
  • 150
  • 1
  • 8
  • I should also say that I can run mongodb locally on my mac just fine and connect to it from the nodejs app just fine. It is specific to the docker container. – Mike Oct 03 '15 at 16:19

1 Answers1

1

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. This mongodb instance does not exist, so it was throwing the error.

I centralized the mongodb config and everything is golden.

Mike
  • 150
  • 1
  • 8