vkarpov15 / run-rs

Zero-config MongoDB runner. Starts a replica set with no non-Node dependencies, not even MongoDB.
Apache License 2.0
265 stars 37 forks source link

ReplicaSetNoPrimary MongooseServerSelectionError when connecting from inside a Docker container #52

Open ghettosamson opened 4 years ago

ghettosamson commented 4 years ago

Environment: MacOS Issue: I have a NodeJS application that I want to run inside a Docker container to connect to the replica set using mongoose 5.10.6. I start my replica set with run-rs --dbpath ./data --keep. When I run my application outside of a Docker container node server I connect successfully {"message":"Mongoose successfully connected to mongodb://localhost:27017,localhost:27018,localhost:27019/my-app-dev?replicaSet=rs","level":"info","service”:"my-ap" However, when I run the application inside of a Docker container, I get the following error

info: Mongoose disconnected from mongodb://host.docker.internal:27017,host.docker.internal:27018,host.docker.internal:27019/my-app-dev?replicaSet=rs {"service":"my-app"}
error: Error connecting to the database connect ECONNREFUSED 127.0.0.1:27017 {"service":"my-app","reason":{"type":"ReplicaSetNoPrimary","setName":"rs","maxSetVersion":1,"maxElectionId":"7fffffff0000000000000079","servers":{},"stale":false,"compatible":true,"compatibilityError":null,"logicalSessionTimeoutMinutes":null,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"commonWireVersion":7},"stack":"MongooseServerSelectionError: connect ECONNREFUSED 127.0.0.1:27017\n    at NativeConnection.Connection.openUri (/src/node_modules/mongoose/lib/connection.js:800:32)\n    at /src/node_modules/mongoose/lib/index.js:341:10\n    at /src/node_modules/mongoose/lib/helpers/promiseOrCallback.js:31:5\n    at new Promise (<anonymous>)\n    at promiseOrCallback (/src/node_modules/mongoose/lib/helpers/promiseOrCallback.js:30:10)\n    at Mongoose.connect (/src/node_modules/mongoose/lib/index.js:340:10)\n    at Object.<anonymous> (/src/index.js:46:21)\n    at Module._compile (internal/modules/cjs/loader.js:1138:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)\n    at Module.load (internal/modules/cjs/loader.js:986:32)\n    at Function.Module._load (internal/modules/cjs/loader.js:879:14)\n    at Module.require (internal/modules/cjs/loader.js:1026:19)\n    at require (internal/modules/cjs/helpers.js:72:18)\n    at Object.<anonymous> (/src/server.js:3:13)\n    at Module._compile (internal/modules/cjs/loader.js:1138:30)\n    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)"}

Here is my Dockerfile

FROM node:12-alpine

LABEL maintainer="***" version="0.0.1"

WORKDIR /src

# Alpine image does not come with bash
RUN apk add --no-cache bash

COPY package.json /src/
RUN npm install
COPY . /src

EXPOSE 3000

CMD [ "npm", "start"]
mexin commented 4 years ago

I think your problem is because you are only opening port 3000, can you try changing EXPOSE 3000 to EXPOSE 3000 27017 27018 27019 see if that does the trick.

ghettosamson commented 4 years ago

I updated the run command to bind ports 27017, 27018, 27019 and also the EXPOSE directive in the Dockerfile but I still get the connection refused.

mexin commented 4 years ago

I got mine running, try the following when you run the container: docker run -ti -p 27017-27019:27017-27019 --network="host" --name mongo-runrs YOURIMAGEID If you don't define --network="host" then you won't be able to connect to the container using localhost.

Hope this helps.

ghettosamson commented 4 years ago

I still get the same error. Note that when we use "host" network mode the published ports are discarded.

docker run -it -p 27017-27019 --network="host" --name my-api --env-file RS.env my-api
WARNING: Published ports are discarded when using host network mode

> my-api@0.2.21 start /src
> node --inspect server

Debugger listening on ws://127.0.0.1:9229/fe5f0b84-faa4-40eb-bb0f-037ccb27f3fd
For help, see: https://nodejs.org/en/docs/inspector
debug: Mongoose connecting to mongodb://localhost:27017,localhost:27018,localhost:27019/my-db?replicaSet=rs with options  {"useCreateIndex":true,"useNewUrlParser":true,"useUnifiedTopology":true,"useFindAndModify":false,"timestamp":"2020-10-27T12:06:34.295Z"}
info: Request logging set to: false {"timestamp":"2020-10-27T12:06:34.414Z"}
info: MY API Express app starting on port 3000 with log level silly {"timestamp":"2020-10-27T12:06:34.415Z"}
error: UnhandledRejection Error: connect ECONNREFUSED 127.0.0.1:27017 {"timestamp":"2020-10-27T12:07:04.311Z"}
error: connect ECONNREFUSED 127.0.0.1:27017 {"reason":{"type":"ReplicaSetNoPrimary","setName":"rs","maxSetVersion":1,"maxElectionId":"7fffffff00000000000000a5","servers":{},"stale":false,"compatible":true,"compatibilityError":null,"logicalSessionTimeoutMinutes":null,"heartbeatFrequencyMS":10000,"localThresholdMS":15,"commonWireVersion":7}}
anasanzari commented 3 years ago

I'm facing the same issue, where client is disconnected arbitrarily saying MongooseServerSelectionError: connection timeout.