typegoose / mongodb-memory-server

Manage & spin up mongodb server binaries with zero(or slight) configuration for tests.
https://typegoose.github.io/mongodb-memory-server/
MIT License
2.57k stars 185 forks source link

Unable to connect to replica set running within Docker container #814

Closed diegosasw closed 11 months ago

diegosasw commented 11 months ago

Versions

package: mongo-memory-server

What is your question?

When creating the 1 node replica set with the node application I can connect properly. I have dockerized it and, it spins up the container, but my MongoDB Client (Robo 3T) is unable to connect. It times out

I have configured it to bing all IP addresses

import { MongoMemoryReplSet } from "mongodb-memory-server";

// This will create an new instance of "MongoMemoryReplSet" and automatically start all Servers
const replset = await MongoMemoryReplSet.create({
  instanceOpts: [
    {
      port: 27017,
      ip: `::,0.0.0.0`,
      storageEngine: "wiredTiger"
    }
    // each entry will result in a MongoMemoryServer (replSet.count will not count towards here)
  ],
  replSet: {
    name: "rs",
    count: 1,
    storageEngine: "wiredTiger" // by default was ephermeralForTest
  }
});

console.log("Replica set is running...");
const uri = replset.getUri();
console.log(`Connect using: ${uri}`);

// The ReplSet can be stopped again with
//await replset.stop();

// Listen for termination signals
process.on("SIGINT", gracefulShutdown);
process.on("SIGTERM", gracefulShutdown);

function gracefulShutdown() {
  console.log("\nReceived shutdown signal. Stopping replica set...");
  replset.stop().then(() => {
    console.log("Replica set stopped. Exiting...");
    process.exit(0);
  });
}

But, for some reason, from my OS (Windows) I can sping up the docker container, I can see that the MongoDB is running inside properly but I can't connect from outside.

It can be reproduced here at this public repository: https://gitlab.com/sunnyatticsoftware/mongodb-rs/-/tree/8740a5146ad19bb8a1386f2ed2d883c6a20818cc

Spin up the docker container with this

docker run --name mongodbrs -p 27017:27017 registry.gitlab.com/sunnyatticsoftware/mongodb-rs

And the MongoDB replicaset will run with connection string mongodb://127.0.0.1:27017/?replicaSet=rs

Timed out after 30000 ms while waiting for a server that matches com.mongodb.client.internal.MongoClientDelegate$1@5ffb8bd6. Client view of cluster state is {type=REPLICA_SET, servers=[{address=127.0.0.1:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketReadException: Prematurely reached end of stream}}]

Inside the container, I can see mongo is up and running, as I can install mongosh and do the following

# mongosh --host localhost --port 27017
Current Mongosh Log ID: 652ea8f840f790f5d361ca6d
Connecting to:          mongodb://localhost:27017/?directConnection=true&serverSelectionTimeoutMS=2000
Using MongoDB:          6.0.9
Using Mongosh Beta:     0.12.1

For mongosh info see: https://docs.mongodb.com/mongodb-shell/

To help improve our products, anonymous usage data is collected and sent to MongoDB periodically (https://www.mongodb.com/legal/privacy-policy).
You can opt-out by running the disableTelemetry() command.

------
   The server generated these startup warnings when booting:
   2023-10-17T15:09:25.955+00:00: You are running this process as the root user, which is not recommended
   2023-10-17T15:09:25.956+00:00: /sys/kernel/mm/transparent_hugepage/enabled is 'always'. We suggest setting it to 'never'
   2023-10-17T15:09:25.956+00:00: vm.max_map_count is too low
------

rs [direct: primary]> 

I can also check the status

rs [direct: primary]> db.serverStatus()
...
host: '3fb2dd3f9ea2',
  version: '6.0.9',
  process: 'mongod-x64-debian-6.0.9',
  pid: 18,
  uptime: 1787,
  uptimeMillis: 1787659,
  uptimeEstimate: 1787,
  localTime: 2023-10-17T15:39:12.845Z,
...

Any help would be appreciated.

diegosasw commented 11 months ago

False alarm, I had made a mistake with ip configuration. It's connecting properly with IP binding to 0.0.0.0 https://gitlab.com/sunnyatticsoftware/mongodb-rs/-/tree/05af30bec429aa8fd0e93b3874819c6572fb28d8