Why Can’t I Connect Compass to My Docker MongoDB Cluster?

0
4
Asked By CuriousCat42 On

I'm having trouble connecting MongoDB Compass to my Docker Compose MongoDB cluster. Here's the Docker Compose setup I'm using:

```yaml
version: '3.8'

services:
mongo1:
image: mongo:5
container_name: mongo1
ports:
- "27017:27017"
command: ["mongod", "--replSet", "myReplicaSet", "--bind_ip_all"]
networks:
- mongoCluster

mongo2:
image: mongo:5
container_name: mongo2
ports:
- "27018:27017"
command: ["mongod", "--replSet", "myReplicaSet", "--bind_ip_all"]
networks:
- mongoCluster

mongo3:
image: mongo:5
container_name: mongo3
ports:
- "27019:27017"
command: ["mongod", "--replSet", "myReplicaSet", "--bind_ip_all"]
networks:
- mongoCluster

rs-init:
image: mongo:5
container_name: rs-init
depends_on:
- mongo1
- mongo2
- mongo3
networks:
- mongoCluster
entrypoint:
- sh
- -c
- |
echo 'Waiting for MongoDB containers to be ready...'
until mongo --host mongo1 --eval "db.adminCommand('ping')" > /dev/null 2>&1; do
echo "Waiting for mongo1..."
sleep 2
done
echo 'MongoDB is up. Initiating replica set...'
mongo --host mongo1 --eval "
rs.initiate({
_id: 'myReplicaSet',
members: [
{ _id: 0, host: 'mongo1:27017' },
{ _id: 1, host: 'mongo2:27017' },
{ _id: 2, host: 'mongo3:27017' }
]
});
rs.status();
"
echo 'Replica set initiated.'
tail -f /dev/null

networks:
mongoCluster:
driver: bridge
```

Currently, I'm using the URI `mongodb://localhost:27017` for Compass. I also need to ensure that this setup works across all operating systems (Windows, Linux, macOS) without making specific configuration changes like updating the /etc/hosts file.

2 Answers

Answered By Wanderer228 On

If you're trying to connect without changing the /etc/hosts file, consider exposing your containers to public IPs instead. You might want to adjust the binding in your Docker Compose file and test connecting via those addresses.

CodeMaster11 -

Also worth noting that firewall settings could affect the connection; just keep that in mind.

SkyWalker88 -

Good idea! I’m exploring different binding options, thanks!

Answered By ExplorerMax On

Make sure to check the connection settings in Compass. You might be able to use the IP address of your Docker host instead of 'localhost'. If you're on macOS, try using `host.docker.internal` as the host in your URI.

TechyTom -

Also, check if your MongoDB containers are running properly. If they’re up, you should see some logs in the terminal for each service.

DevNinja99 -

Thanks for the tip! I'm on a Windows machine, so I'll give that a shot.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.