Home > Mobile >  Connect to Mongo Router inside Docker container from remote host
Connect to Mongo Router inside Docker container from remote host

Time:09-22

I have an EC2 instance in AWS with a mongo docker container that init a mongo router such as :

docker run --network mongo-net -p 27051:27017 --expose=27017 -d --name=mongos51 mongo mongos --port 27017 --configdb configserver/configserver41:27017,configserver42:27017 --bind_ip 0.0.0.0

I have opened my SG in AWS to allow any connections and i can reach the public IP on the port 27051 without problems.

My container is in a bidge docker network to allow all of my mongo instances to communicate.

However, if i try to connect to the mongo shell like :

mongosh mongodb://public_ip:27051 

I have the error Connection refused instantly. Same if i try to open the mongoshell from inside the container with :

docker exec -it mongos51 bash -c "echo 'sh.addShard(\"shard1/mongodb11\")' | mongosh "
Current Mongosh Log ID: 6149ed68def89bc1907c8988
Connecting to:          mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000
MongoNetworkError: connect ECONNREFUSED 127.0.0.1:27017

I have the same error if i use the private IP instead of 127.0.0.1.

What i am missing here ?

CodePudding user response:

According the documentation try to connect like this :

mongosh --host public_ip --port 27051

It work for me when I follow these steps.

  • Related