Unable to connect to MongoDB through docker-compose network (configured with replicaset)
See original GitHub issueBug description
Hi! I think I have an issue that is probably related to this one https://github.com/prisma/prisma/issues/11860. Prisma can’t pull/connect from MongoDB, if it is in the other container in the same docker-compose network. Meaning that Prisma won’t connect when I use connection string
DATABASE_URL="mongodb://mongo:27017/mongo"
instead of
DATABASE_URL="mongodb://localhost:27017/mongo"
even though this connection string would work with other clients (mongosh, MongoDB for VSCode in dev container)
How to reproduce
Steps to reproduce:
- Spin up docker-compose with:
- a. MongoDB with replica set setup, (e.g. from this docker-compose file https://github.com/prisma/prisma/blob/main/docker/docker-compose.yml)
- b. Node.js container with Prisma and prisma.schema (and optional @prisma/client simple setup)
- Run ```npx prisma db pull`` in Node.js container, it gives error:
Error: Error in connector: Error querying the database: Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: localhost:27018, Type: Unknown, Error: Cannot assign requested address (os error 99) }, ] }
- Or start Node.js with @prisma/client, it gives similar error:
PrismaClientInitializationError: Database error. error code: unknown, error message: Server selection timeout: No available servers. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: localhost:27017, Type: Unknown, Error: Cannot assign requested address (os error 99) }, ] }
at /app/node_modules/@prisma/client/runtime/index.js:36300:21 {
clientVersion: '3.9.2',
errorCode: undefined
}
- Finally, make sure that the connection string is valid if used through the other MongoDB client from the same container. E.g. install
mongosh
to the interactive Node.js container (https://docs.mongodb.com/mongodb-shell/install/) and connect with
mongosh mongodb://mongo:27017/mongo
- Also, when connecting to the same db from the Prisma installed at host machine with DATABASE_URL=“mongodb://localhost:27017/mongo” - everything works just fine.
Expected behavior
Prisma should be able to connect to the MongoDB, within docker-compose network, with the same connection string mongodb://mongo:27017/mongo as the other clients would (e.g. mongosh). There should be no difference between connecting to the mongodb://localhost:27017/mongo from the host machine or connecting to the mongodb://mongo:27017/mongo from the other container in the same docker-compose network.
Prisma information
datasource db {
provider = "mongodb"
url = "mongodb://root:prisma@mongo:27018/admin"
}
generator client {
provider = "prisma-client-js"
previewFeatures = ["mongodb"]
}
Environment & setup
- OS: Mac OS, Ubuntu 20
- Database: MongoDB 4
- Node.js version: 16
Prisma Version
3.10.0-dev.66
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (2 by maintainers)
Top GitHub Comments
I was able to resolve it changing env variable in docker-compose (https://github.com/prisma/prisma/blob/main/docker/docker-compose.yml):
From:
To:
Yes, it seems so. Also, later on, I found out that it also works fine with
However, with this setup I can’t connect to the Mongo from host machine via the
mongodb://localhost:27018/mongo
, whatever the client. Which is not a big problem as far as MongoDB is easily reachable within docker-compose network, and which is not related to Prisma, but only to the dev setup.