How to share one Mongo database with multiple Meteor apps
When I needed to share a database between app #1 and app #2, the solution was astonishingly hard to accomplish. Please let me know if there's a better way to do this.
Update I did find a better approach to this, and I switched our infrastructure to a more common "database and app are separate servers" paradigm. Take a look at my new in depth tutorial on setting up mongodb instead!
Since I need to share a DB, the very first thing I did was install the latest Mongo. My idea was that I'd turn off MUP's mongo autoinstall and manually set both MONGO_URL's to the same URL and instantly have both meteor sites connect to that same DB.
Simple, enough, except now that we use Docker, our apps live in separate containers! After a day of searching I found out Docker's default network mode is BRIDGE which puts each container on its own IP. That's wonderful for many reasons! But I can't access the host mongo, plus that's extra router virtualization for no reason. To be clear, the problem is when MONGO_URL is localhost, it tries to connect to the container's localhost and not the host's localhost.
I ended up changing how mup deploy works.
First modification was in /lib/modules/meteor/assets/templates/start.sh I knew I didn't much care for the virtual networking, so...
On line 22 I added --net="host" and removed --hostname and --publish Now it's practically done! Just fails the post deploy check. A wonderful hint appeared that made it clear what it was doing. It was checking for the app to be running at localhost:80. Now, that would work in a bridge configuration but I want port 3000 (specified in the deploy config file). So I did a search for the string "80" went and modified /lib/modules/meteor/index.js
Thank god Arounda put in this comment on line 127.
// sending PORT to the docker container is useless. // It'll run on PORT 80 and we can't override it // Changing the port is done via the start.sh scriptYou are my hero for commenting your code well.
On line 130 I simply removed delete env.PORT so mup would know where to check (localhost:3000 in my case).
And that did the trick. app #1 is set to run on 3000 and app #2 is set to run on 4000
All that's left is setting up NGINX so that they can be nice pretty domain names instead of port numbers and I can throw in some A+ grade SSL too.
Unfortunately although I like my solution, today was my first day dealing with Docker. Editing this script means I have to maintain this code forever and I daresay I want to maintain more than I have to.