5

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)

Mike
  • 3,775
  • 8
  • 39
  • 79

1 Answers1

6

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 > 
Community
  • 1
  • 1
Mike Stankavich
  • 2,709
  • 1
  • 16
  • 8
  • thanks. I have tried the meanjs template as well and it connects fine now. I only have dns problems now. Where does the app show in your case (domain name, port)? – Mike Sep 13 '14 at 17:31
  • I was able to enable virtualhost naming for apps and set up a wildcard DNS entry for *.app.domain.com, so my app shows up at http://test.app.domain.com. This article https://www.andrewmunsell.com/blog/dokku-tutorial-digital-ocean was very helpful. – Mike Stankavich Sep 20 '14 at 17:26
  • Is there anyway i can connect to the mongoDB instance created via the MongoDB plugin via an external shell? e.g. my terminal from my local computer? using the MONGO_URL? – Frank Fu Feb 23 '15 at 12:37
  • 1
    Yes if you look at docker ps you will see the port that gets mapped to the process within the docker container. I don't have a system running right now so I can't check, but I think it was in the 47000 range somewhere – Mike Stankavich Mar 05 '15 at 04:22
  • Connect to your droplet's public IP. For the port, use dokku mongo:list, you will get 5 NNNNN->MMMMM pairs, use the first MMMMM – malix Feb 19 '16 at 16:22
  • NOTE (to future self or others): make sure to use dokku mongo:link mongoname appname – malix Feb 19 '16 at 16:28
  • So how do you switch your config between your dokku server and your local server? – AlxVallejo Jun 22 '16 at 02:13
  • If you are using environment variables to hold environment-specific config values, simply set the environment variables on your local to point to the corresponding config values for local development. Local should just be one more environment whether or not it's running in a container. – Mike Stankavich Jun 22 '16 at 17:42