zodern / meteor-up

Production Quality Meteor Deployment to Anywhere
http://meteor-up.com/
MIT License
1.27k stars 281 forks source link

Connect to a mongodb running in the docker host #1255

Closed ArosPrince closed 3 years ago

ArosPrince commented 3 years ago

Hi,

in my scenario I have a DB that I need to connect to from two different applications. One is the Meteor app I am just trying to deploy, and another one is a different backend only nodejs app.

When I try to deploy using MeteorUp, I get the following error:

/built_app/programs/server/node_modules/fibers/future.js:313
                                                        throw(ex);
                                                        ^

        MongoServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017
            at Timeout._onTimeout (/built_app/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb/lib/core/sdam/topology.js:439:30)

I guess the reason is really that there already is a mongo service running on the machine I am trying to deploy to (for the reasons stated above - there's the second app already using the DB).

Now, locally on the dev computer this works OK, I just set up the MONGO_URL environment variable to point to that shared DB.

But in the MeteorUp documentation, there's this sentence:

We use a mongodb docker container to run the local MongoDB data (it uses the old MongoDB location)

...and I cannot see any way how to connect to a mongo service already running on that machine.

Am I misunderstanding something, or do I actually correctly get what this is about? And if so, is there a way then how I can connect to a local DB?

//edit: Forgot to mention... Yes I did remove the mongo object from mup.js. But that did not help.

Thanks!

ArosPrince commented 3 years ago

I might have misunderstood the message. Possibly the problem may just be that the meteor app runing inside a docker container is not able to access a db running on the docker host. If this was mac one could allegedly use host.docker.internal instead of localhost. But this is Ubuntu... Will try to dig in.

//edit: I am still not sure, but I think the problem is with the local mongo configuration. Unless I fill in bind_ip 0.0.0.0 (which I obviously don't want in the end) and set up MONGO_URL in mup.js with the global IP of the server, it will never connect.

Any idea how to set it up so that the app runing in the MeteorUp docker connects to the DB running on the docker host (Ubuntu server as required by MeteorUp) without using it's global IP and exposing the DB to all the interfaces (0.0.0.0)?

ArosPrince commented 3 years ago

OK, after some long hours of swearing I found a solution:

Use this as a MONGO_URL: mongodb://host.docker.internal:27017/dbname

Then, add this to the docker.args array: --add-host host.docker.internal:host-gateway.

So in my case, it's the following setup:

    docker: {
      image: 'zodern/meteor',
      args: [
        '--add-host host.docker.internal:host-gateway'
      ],
      prepareBundle: true
    },

And then there's one more thing you need to take care of. Since the connection to the mongodb is now not comming from the actual localhost but from the docker container, you need to update bindIp in you mongo configuration (/etc/mongod.conf). That means adding the docker interface also.

net:
  port: 27017
  bindIp: 127.0.0.1,172.17.0.1

And that's it. I hope this will be useful to someone.

Sorry for creating this issue in the first place. As I said I misunderstood the message.