title: NodeJS / Express cannot connect to mongodb running in docker
body: <p>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.</p>
  
  <p>I have enabled port forwarding via the -p arg to docker run.</p>
  
  <pre><code>docker run -p 27017:27017 --name test -d mongo:2.6
  60b7f3cf658bc75ceca2eae074e5f694ec622f7487b6a2f36fa02edd4af68357
  </code></pre>
  
  <p>I can see it running.</p>
  
  <pre><code>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-&gt;27017/tcp   test
  </code></pre>
  
  <p>If I run docker logs, I see that it is listening on port 27017.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>When I attempt to connect from express, I get the following error.</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>There are some strange things also...</p>
  
  <ul>
  <li>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.</li>
  <li>I see the connection from my nodejs app in the logs as well, but then an immediate disconnect.</li>
  </ul>
  
  <p><br/></p>
  
  <pre><code>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)
  </code></pre>
  
  <p>See also:</p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/25828021/unable-to-connect-to-mongodb-running-in-docker-container">Unable to connect to mongoDB running in docker container</a></li>
  <li><a href="https://stackoverflow.com/questions/29999213/node-express-app-cant-connect-to-docker-mongodb">Node / Express app can&#39;t connect to docker mongodb</a></li>
  </ul>