Search code examples
node.jsmongodbdigital-oceandokku

deploying node.js app with mongodb with dokku on digital ocean


I am trying to deploy a Node.js app with mongodb on digital ocean with dokku. Unfortunately, I have some problems having the node app connecting to mongodb.

What have I done so far. 0. I have a node.js app in a git repo 1. Created dokku instance in digital ocean (runs on ubuntu 14.04) 2. I created a dokku-mongodb-plugin (see https://github.com/jeffutter/dokku-mongodb-plugin): follow the git clone etc installation instructions over there. 3. push my app to the digital ocean server (git push -u production master) 4. try to create a mongodb process: (on server): dokku mongodb:create testapp (testapp is the name of my app) 5. run my app: dokku run testapp node app.js

This leads to a connection error (at the bottom)

I have tested: dokku mongodb:list

and it does not return anything, which makes me think there might be an issue with the plugin?

Otherwise, I suspect it might be due to mongodb running under a different ip / process than the standard localhost.

Anyway, I am not really sure what the problem is. Any help would be appreciated.

Cheers, Mike

vents.js:72 throw er; // Unhandled 'error' event ^ Error: failed to connect to [localhost:27017] at null. (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/server.js:546:74) at emit (events.js:106:17) at null. (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection_pool.js:150:15) at emit (events.js:98:17) at Socket. (/app/node_modules/mongoose/node_modules/mongodb/lib/mongodb/connection/connection.js:533:10) at Socket.emit (events.js:95:17) at net.js:440:14 at process._tickCallback (node.js:419:13)


Solution

  • The dokku mongodb plugin uses an RFC 1918 private network address for its mongodb docker container. As noted in this article How to get a Docker container's IP address from the host? you can get the address from docker inspect. Get the container id from docker ps for the dokku-mongodb container, then run dokku inspect with that container id.

    When you use dokku mongodb:link < app > < database > or specify the app name when creating the database that sets a series of mongodb environment variables in your app's dokku config, which you can view by running dokku config < app >.

    $ dokku config test
    === test config vars ===
    NODE_ENV:         test
    MONGODB_DATABASE: "myapp-test-db-production"
    MONGODB_HOST:     "172.17.0.123"
    MONGODB_PORT:     "27017"
    MONGODB_USERNAME: "test"
    MONGODB_PASSWORD: "***********************"
    MONGO_URL:        "mongodb://test:**********************@172.17.0.123:27017/myapp-test-db-production"
    

    You'll need to reference those environment variables from your mongodb connection code. In my case, I'm using the meanjs.org template, so I set the value for db in config/env/test.js to db: process.env.MONGO_URL, and I added NODE_ENV=test with dokku config:set.

    'use strict';
    
    module.exports = {
        db: process.env.MONGO_URL,
        // db: 'mongodb://localhost/mean-test',
    < snip >