Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ReplicaSetNoPrimary MongooseServerSelectionError when connecting from inside a Docker container #52

Open
ghettosamson opened this issue Oct 8, 2020 · 5 comments

Comments

@ghettosamson
Copy link

ghettosamson commented Oct 8, 2020

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
Copy link

mexin commented Oct 20, 2020

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
Copy link
Author

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
Copy link

mexin commented Oct 21, 2020

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
Copy link
Author

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

> [email protected] 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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants