I saw several questions similar to mine, but I wasn't able to solve my problem based on what I've seen so I am writing my own.
I have a Docker-compose.yaml file where we have this configuration:
mongo-rs:
image: mongo:4.4.21
restart: always
container_name: mongo-rs
command: ["--replSet", "rs0", "--bind_ip_all", "--port", "30001"]
volumes:
- ~/.docker/mongo-rs/data/:/data/db
ports:
- 30001:30001
healthcheck:
test: test $$(echo "rs.initiate({_id:'rs0',members:[{_id:0,host:\"mongo-rs:30001\"}]}).ok || rs.status().ok" | mongo --port 30001 --quiet) -eq 1
interval: 10s
start_period: 30s
mongo-express-rs:
image: mongo-express:0.54.0
restart: always
ports:
- 8086:8081
environment:
ME_CONFIG_MONGODB_SERVER: mongo-rs
ME_CONFIG_MONGODB_PORT: 30001
In Docker Desktop I can see that the container is up and running. I am able to connect to Mongo Express http://localhost:8086/
but when I open MongoDB Compass I am unable to connect. I tried several connection strings but I am getting error no matter what I am using:
Given this how can I connect to the Mongo instance in Docker?
In the case we use Mongo-rs instead of the standard MongoDB. This is mainly when we need the to transactions working with Mongo. However this requires a change it the connection string which becomes something like this:
mongodb://localhost:30001/?directConnection=true
In my case this was all I need to get things up and running an I hope that this will save some time to other people getting the same issue as me.