question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to connect to MongoDB through docker-compose network (configured with replicaset)

See original GitHub issue

Bug 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:

  1. Spin up docker-compose with:
  1. 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) }, ] }
  1. 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
}
  1. 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
  1. 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:closed
  • Created 2 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

4reactions
hermanmitishcommented, Feb 20, 2022

I was able to resolve it changing env variable in docker-compose (https://github.com/prisma/prisma/blob/main/docker/docker-compose.yml):

From:

  # Replica Set (required for Prisma Client)
  mongo:
    build: ./mongodb_replica
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: prisma
      MONGO_REPLICA_HOST: localhost
      MONGO_REPLICA_PORT: 27018
    ports:
      - '27018:27018'

To:

  # Replica Set (required for Prisma Client)
  mongo:
    build: ./mongodb_replica
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: prisma
      MONGO_REPLICA_HOST: host.docker.internal
      MONGO_REPLICA_PORT: 27018
    ports:
      - '27018:27018'
1reaction
hermanmitishcommented, Feb 21, 2022

Yes, it seems so. Also, later on, I found out that it also works fine with

  mongo:
    build: ./mongodb_replica
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: prisma
      MONGO_REPLICA_HOST: mongo
      MONGO_REPLICA_PORT: 27018
    ports:
      - '27018:27018'

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

MongoDB Replica Docker: Cannot connect on replica, only ...
When connecting to a replica set the client has to be able to connect to each node as defined in the replica set...
Read more >
Docker container unable to connect to MongoDB Replica set
1 Answer 1 · Make sure the hostnames used in your replica set config ( Mongo1 , Mongo2 , Mongo3 ) can be...
Read more >
Docker container unable to connect to MongoDB Replica set
Make sure the hostnames used in your replica set config ( Mongo1 , Mongo2 , Mongo3 ) can be resolved in the container...
Read more >
Cannot connect from node to mongo replicaset in docker
1 Answer 1 · I see what is wrong now. Your containers are not linked or in the same docker network thus you...
Read more >
How to deploy a MongoDB replica set using docker-compose
We will use as the main source of information the official MongoDB documentation that can be found in the reference section. Without further...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found