I’m trying to set up a MongoDB replica set using Docker and connect to it with Prisma.
My setup:
Docker Compose file:
I have the following docker-compose.yml file for the MongoDB replica set:
version: "3.8"
services:
mongo1:
image: mongo:latest
container_name: mongo1
ports:
- 27017:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
command: ["mongod", "--replSet", "rs0"]
networks:
- mongo-network
mongo2:
image: mongo:latest
container_name: mongo2
ports:
- 27018:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
command: ["mongod", "--replSet", "rs0"]
networks:
- mongo-network
mongo3:
image: mongo:latest
container_name: mongo3
ports:
- 27019:27017
environment:
- MONGO_INITDB_ROOT_USERNAME=root
- MONGO_INITDB_ROOT_PASSWORD=password
command: ["mongod", "--replSet", "rs0"]
networks:
- mongo-network
prisma:
image: prisma:latest
depends_on:
- mongo1
- mongo2
- mongo3
networks:
- mongo-network
networks:
mongo-network:
driver: bridge
Replica Set Initialization:
I connected to mongo1 and initialized the replica set with this command:
rs.initiate({
_id: "rs0",
members: [
{ _id: 0, host: "mongo1:27017" },
{ _id: 1, host: "mongo2:27017" },
{ _id: 2, host: "mongo3:27017" }
]
});
Prisma .env file:
My DATABASE_URL is set as follows:
DATABASE_URL="mongodb://root:password@mongo1:27017,mongo2:27017,mongo3:27017/mydatabase?replicaSet=rs0"
Prisma schema:
Here is a part of my Prisma schema:
datasource db {
provider = "mongodb"
url = env("DATABASE_URL")
}
model Category {
id String @id @default(uuid()) @map("_id")
name String @unique
courses Course[]
}
What I’ve tried:
Checked that all containers are up and running with docker ps.
Confirmed that the mongo1, mongo2, and mongo3 services are accessible by pinging their names from within other containers in the same network.
Verified the replica set initialization by connecting to mongo1 and running rs.status().
Restarted the containers with docker-compose down && docker-compose up -d.
The issue:
Prisma still fails to connect to the MongoDB replica set, throwing the error about “failed to lookup address information”.
What could be causing this issue, and how can I ensure that Prisma connects to my MongoDB replica set running in Docker?
JogenPJ is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.