robinnnnn robinnnnn - 4 months ago 15x
Node.js Question

Accessing the same mongoDB instance from multiple Docker containers

I have two containers (A and B) running Node + MongoDB.

contains restricted endpoints that require a valid token in order to read + write from the database.
will contain public endpoints that have no request requirements and allow anyone to read certain documents from the database.

The endpoints exposed in both
should interact with the same database
. I'm not sure how to do this when it comes to MongoDB.

I'm defining the schema / model for the collection in container
, like so:

const mongoose = require('mongoose');
const Project = new mongoose.Schema({ ... });

From container
, all I want to do is to be able to sift through all Projects in the database. I don't think I'm supposed to create the same schema in this new container.

It's important to note that container B is successfully connected to the same DB as container A. It's just a matter of sifting through it via mongoose.

TL;DR: How can I access container A's db from container B, where I haven't registered any models? I'm pretty new to this so any info would be helpful.


you can use docker-compose.yml like

mongodb: image: mongo dockera: links: - mongodb:mongodb dockerb: links: - mongodb:mongodb

then you containera and containerb will connect the same mongodb