Since the original question basically is “How can I access a replica set inside a docker network from the host”, this solution might be useful:

  1. Create a public DNS entry to a “localhost” host on domain you own, for example localhost.somedomain.com → 127.0.0.1
  2. Use this single host in the replica set config as name (use only one mongo in the set)
  3. In your docker setup, override the DNS with the docker host running the replica. In docker-compose, the network aliases section can be used for this

This will result in name resolve allowing access to mongo via localhost.somedomain.com both from inside docker and from the host (given the right exposed port)

Note that the usual solution to this problem is “modify your local /etc/hosts file”, which works too, but requires every dev/system in your organization to modify system files.

1 Like