Reputation: 21
I am running docker for mac 1.12.0 and trying to connect to a mongo replica set running on my mac. I am using mongo gem in ruby to connect and also mentioned the --net="host"
option while doing a docker run
. I get an error like this:
Mongo::ConnectionFailure: Cannot connect to a replica set using seeds 127.0.0.1:27017, 127.0.0.1:27018 /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/connection/pool_manager.rb:272:in
get_valid_seed_node' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/connection/pool_manager.rb:178:in
connect_to_members' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/connection/pool_manager.rb:70:inblock in connect' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/connection/pool_manager.rb:65:in
synchronize' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/connection/pool_manager.rb:65:inconnect' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_replica_set_client.rb:210:in
block in connect' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_replica_set_client.rb:199:insynchronize' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_replica_set_client.rb:199:in
connect' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_client.rb:656:insetup' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_replica_set_client.rb:524:in
setup' /home/ubuntu/.rvm/gems/ruby-2.1.8@automator/gems/mongo-1.12.5/lib/mongo/mongo_replica_set_client.rb:176:ininitialize
I ran the container in daemon mode and tried to ping my host ip and it was successful. So technically it should work but it isn't. Anybody else faced a similar situation ?
Upvotes: 2
Views: 310
Reputation: 9934
Instead of using you macs host-ip, which would be not portable (a different developer, a different network like office/home), use this approach: https://gist.github.com/EugenMayer/3019516e5a3b3a01b6eac88190327e7c
So you create an alias-ip for your loopback device on your mac. Then, instead of connecting to 127.0.0.1 in your container, use 10.254.254.254.
A very similar question with a more briefly answer here https://stackoverflow.com/a/38985163/3625317
Upvotes: 0
Reputation: 4571
Based on the error you are getting your application is trying to connect MongoDB locally (127.0.0.1
), which means inside docker container. Obtain IP address of your mac host (where the MongoDB runs) and set it in your application config.
Consider also running MongoDB in another docker container with help of docker compose
.
Upvotes: 1