I have a following MongoDB docker cluster as defined in the following docker-compose.yml
:
version: "3"
services:
mongo1:
hostname: mongo1
container_name: mongo1
image: mongo:5
volumes:
- ${PWD}/data/db/mongo1:/data/db
expose:
- 27017
ports:
- "27011:27017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo2:
hostname: mongo2
container_name: mongo2
image: mongo:5
volumes:
- ${PWD}/data/db/mongo2:/data/db
expose:
- 27017
ports:
- "27012:27017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
mongo3:
hostname: mongo3
container_name: mongo3
image: mongo:5
volumes:
- ${PWD}/data/db/mongo3:/data/db
expose:
- 27017
ports:
- "27013:27017"
restart: always
entrypoint: [ "/usr/bin/mongod", "--bind_ip_all", "--replSet", "rs0" ]
I start the cluster as follows:
$ mkdir data
$ docker-compose up -d
I initiate the replica set as follows:
$ docker exec -it mongo1 mongosh --eval "rs.initiate({
_id: \"rs0\",
members: [
{_id: 0, host: \"mongo1\"},
{_id: 1, host: \"mongo2\"},
{_id: 2, host: \"mongo3\"}
]
})"
I can connect to it using Mongo Shell as follows:
$ mongosh "mongodb://localhost:27011/my_db"
However, specifying replicaSet
in the connection string doesn't work the host mongo1
cannot be resolved.
$ mongosh "mongodb://localhost:27011/my_db?replicaSet=rs0"
Current Mongosh Log ID: 62b54fa6573be03f584017b8
Connecting to: mongodb://localhost:27011/my_db?replicaSet=rs0&serverSelectionTimeoutMS=2000&appName=mongosh+1.5.0
MongoServerSelectionError: getaddrinfo EAI_AGAIN mongo1
Neither does mongodb://localhost:27011,localhost:27012,localhost:27013/my_db
work, same error as above.
I also cannot connect to it using MongoDB Compass with the connection string mongodb://localhost:27011/my_db
that works with Mongo Shell.
I suppose I can use network_mode: "host"
in docker-compose config to alleviate the host discovery issue, but I would rather not do it.
Thanks!