I want to deploy a MongoDB Cluster for testing on 3 Virtual Machines using Docker.

On the first Virtual Machine I have the Router Instance and a Replica Set of the 3 Config Servers.

The second Virtual Machine is hosting a Replica Set of three MongoDB Server. They where successfully initiated and elected a Primary.

The third Virtual Machine is hosting a Replica Set of three MongoDB Server. They where successfully initiated and elected an Primary.

When I start my Container which contains the Router it connects successfully to the Replicaset of the Config Server.

If I now want to add my 2 Replica Sets as Shards with the following Command:


rep1 is the name of the Replica Set. rep1-node1 is the hostname for the Container with the Primary Node. (Host Name resolution works fine.)

After a short time I get the Following Error:

    "code" : 133,
    "ok" : 0,
    "errmsg" : "could not find host matching read preference { mode: \"primary\" } for set rep1"

I Have found another question here with the same topic but I don't get any errors with missing Authentication Key. None of my Mongo Instance uses any authentication because it's only a testing environment.

Adding normal Mongo Servers works without any problems. Only Replicasets won't work. I'm using MongoDB version 3.4.

  • Hi Kevin, and welcome to the site. Have you double-checked the health of replica set rep1? It would be useful if you could add the output of rs.status() into your question, because that is likely to contain clues as to what is going wrong. Commented Apr 18, 2017 at 12:18
  • Check log-files of mongos and (primary) config server. I guess that there is complain that connection to rep1-node1. (is that rep1-node1 primary?) Normally when you addShard, you add all nodes at replica set (sh.addShard("rep1/rep1-node1:27018,rep1-node1:27019,rep1-node1:27020") ). When you are at node where you have mongos and CSRS, can you connect directly rep1-node1 (mongo rep1-node1:27018)
    – JJussi
    Commented Apr 18, 2017 at 15:29
  • JJussi and Vince Bowdren thanks for your comments i have found the issue and will post it as an Answer.
    – Kevin
    Commented Apr 18, 2017 at 15:55
  • Good, then why you wrote that "hostname resolution works fine" at your question?!? ;-)
    – JJussi
    Commented Apr 19, 2017 at 9:53
  • I because i thought it is enough that the Router knows the shards by Name ;-)
    – Kevin
    Commented Apr 19, 2017 at 9:54

1 Answer 1


So i have found my mistake. I had to add the Servernames and IP Addresses to the hosts file of the docker container in which the config Servers runs. After that i could add my Replica Set as a Shard. I will edit this answer this or next week and provide a link to a Blogpost in which i will describe how to make a MongoDB Sharding with Docker on Virtual Machines.

  • I am facing the exact same issue. Did you get a chance to highlight the solution in your blogpost. If yes, cam you please provide the link. Commented May 30, 2021 at 8:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.