0

have been trying to connect my docker node.js app to a mongodb service to no avail. I've created a was liberty bridging application ($BRIDGE_APP) with no war or code that is bound to a mongodb service. It is running with good status. Have to say that the same code is running correctly in my local docker container. I am using mongoose to connect to mongo.The only difference in the code is the way of resolving the mongo connection string:

var DB_CONNECT_STRING = 'mongodb://app:password@127.0.0.1:27017/appname';
if(custom.areWeOnBluemix() && custom.doWeHaveServices())
    DB_CONNECT_STRING = custom.getMongoConnectString();
...
console.log('going to connect to mongo@: ' + DB_CONNECT_STRING);
    var db = mongoose.createConnection(DB_CONNECT_STRING);
    db.on('error', console.error.bind(console, 'connection error:'));
    db.once('open', function (callback) {
      console.log('... db open !!!');
    });

I push my image to bluemix with no issues:

ice --local push $REGISTRY/$ORG/$CONTAINER_NAME

I then check the env vars:

cf env $BRIDGE_APP
System-Provided:
{
  "VCAP_SERVICES": {
    "mongodb-2.4": [
      {
        "credentials": {.....

and then I run my container and bind an ip:

ice run --bind $BRIDGE_APP --name $CONTAINER_NAME -p $PORT $REGISTRY/$ORG/$CONTAINER_NAME:latest
sleep 12
ice ip bind $IP $CONTAINER_NAME

...this is almost completely by the book, but for some reason when I check the logs I'm always getting:

ice logs $CONTAINER_NAME
...
going to connect to mongo@: mongodb://c61deb58-45ea-41....
Example app listening at http://0.0.0.0:8080
connection error: { [MongoError: connect ENETUNREACH] name: 'MongoError', message: 'connect ENETUNREACH' }

I have also tried with mongolab service with no success.

Has anybody somehow eventually tried this type of setup that can provide me some additional clue of what's missing here?

thanking you in advance

1 Answers1

2

It has been my experience that networking is not reliable in IBM Containers for about 5 seconds at startup. Try adding a "sleep 10" to your CMD or ENTRYPOINT. Or set it up to retry for X seconds before giving up.

Once the networking comes up it has been reliable for me. But the first few seconds of a containers life have had troubles with DNS, binding, and outgoing traffic.

I gave a similar answer to a similar question recently. Perhaps your problem is the same as the other poster's.

Community
  • 1
  • 1
esnible
  • 340
  • 2
  • 10
  • that was right, I've forced the server code to wait for 2 minutes before connecting to db, and after that I've got a connection successfully. Have ti test shorter sleeps now, but at least this works. Super thank you. – joão tiago viegas Jul 01 '15 at 22:48