1

I have setup a mongoDB Replicaset (3-repicas) inside Docker Container. I am able to access the replicateset from host machine but unable to access the mongoDB Replicaset from a remote client.

Reference: https://www.sohamkamani.com/blog/2016/06/30/docker-mongo-replica-set/

Scripts to create mongoDB replicaset:

$ docker run -it -v <host mount path>:/data/db -p 30000:30000 --name mongo0 --net my-mongo-cluster -d mongo --replSet my-mongo-set --port 30000
$ docker run -it -v <host mount path>:/data/db -p 30001:30001 --name mongo1 --net my-mongo-cluster -d mongo --replSet my-mongo-set --port 30001
$ docker run -it -v <host mount path>:/data/db -p 30002:30002 --name mongo2 --net my-mongo-cluster -d mongo --replSet my-mongo-set --port 30002
$docker exec -it mongo1 mongo
> rs.initiate({_id: "my-mongo-set", version: 1, members: [
  { _id: 0, host : "mongo0:30000" },
  { _id: 1, host : "mongo1:30001" },
  { _id: 2, host : "mongo2:30002" }
]});

The above command was successful.

I tried accessing the mongo Replicaset from same host outside docker container:

$ mongo mongodb://<PUBLIC_IP>:30000,<PUBLIC_IP>:30001,<PUBLIC_IP>:30002/?replicaSet=my-mongo-set
my-mongo-set:PRIMARY> 

even this worked:

$ mongo mongodb://localhost:30000,localhost:30001,localhost:30002/?replicaSet=my-mongo-set
my-mongo-set:PRIMARY> 

my /etc/hosts file:

$ cat /etc/hosts
127.0.0.1 localhost mongo0 mongo1 mongo2
<PUBLIC_IP> <domain> mongo0 mongo1 mongo2

What is not working: From remote client (shell and MongoDB Compass):

$ mongo mongodb://<PUBLIC_IP>:30000,<PUBLIC_IP>:30001,<PUBLIC_IP>:30002/?replicaSet=my-mongo-set
connecting to: mongodb://<PUBLIC_IP>:30000,<PUBLIC_IP>:30001,<PUBLIC_IP>:30002/?replicaSet=my-mongo-set
2020-06-02T22:56:53.776+0530 I NETWORK  [js] Starting new replica set monitor for my-mongo-set/<PUBLIC_IP>:30000,<PUBLIC_IP>:30001,<PUBLIC_IP>:30002
2020-06-02T22:56:53.796+0530 I NETWORK  [ReplicaSetMonitor-TaskExecutor] Successfully connected to <PUBLIC_IP>:30000 (1 connections now open to <PUBLIC_IP>:30000 with a 5 second timeout)
2020-06-02T22:56:53.796+0530 I NETWORK  [js] Successfully connected to <PUBLIC_IP>:30002 (1 connections now open to <PUBLIC_IP>:30002 with a 5 second timeout)
2020-06-02T22:56:53.805+0530 I NETWORK  [ReplicaSetMonitor-TaskExecutor] changing hosts to my-mongo-set/mongo0:30000,mongo1:30001,mongo2:30002 from my-mongo-set/<PUBLIC_IP>:30000,<PUBLIC_IP>:30001,<PUBLIC_IP>:30002
2020-06-02T22:56:54.315+0530 W NETWORK  [js] Unable to reach primary for set my-mongo-set
2020-06-02T22:56:54.315+0530 I NETWORK  [js] Cannot reach any nodes for set my-mongo-set. Please check network connectivity and the status of the set. This has happened for 1 checks in a row.

2020-06-02T22:56:54.821+0530 W NETWORK  [js] Unable to reach primary for set my-mongo-set
2020-06-02T22:56:54.821+0530 I NETWORK  [js] Cannot reach any nodes for set my-mongo-set. Please check network connectivity and the status of the set. This has happened for 2 checks in a row.

How to resolve this issue?

RajKrishnan
  • 149
  • 4
  • 16

1 Answers1

0

This is because from version 3.6 forward, mongod listens only localhost -address, until you tell it otherwise. Check here! or google "mongod bindIp".

JJussi
  • 1,540
  • 12
  • 12
  • I tried couple of options: 1. bind IP to localhost and publicIP 2. bind IP to 0.0.0.0. In option #1, i am unable to connect to container but in option #2 i was able to connect to replica set but not from remote client. #1 `$ docker run -it -v /mnt/volume_blr1_01/mongo0:/data/db -p 30000:30000 --name mongo0 --net my-mongo-cluster -d mongo --replSet my-mongo-set --bind_ip [localhost, ] --port 30000` #2 `$docker run -it -v /mnt/volume_blr1_01/mongo0:/data/db -p 30000:30000 --name mongo0 --net my-mongo-cluster -d mongo --replSet my-mongo-set --bind_ip 0.0.0.0 --port 30000` – RajKrishnan Jun 04 '20 at 06:19
  • First.. When using bind_ip, the list of IP addresses shall not have space after comma. So NOT "ip1, ip2", it must be "ip1,ip2" and of course because you run mongod inside docker, it must listen that INTERNAL address. Second, have you checked that your Firewall is not preventing connections to those ports from outside? "iptables -L -n" is good command to check... – JJussi Jun 05 '20 at 07:09
  • I didn't give same between ip1 and ip2. I have used ufw to allow access to ports 30000, 30001 and 30002. Still it didn't work. Yes, since i run mongod inside docker, it will listen to internal address but i have exposed a port for external access through which i should connect to the container. As i said, i was able to connect to replicate set from same host but not from remote host. Is it something to do with my entries in /etc/hosts? – RajKrishnan Jun 05 '20 at 09:28
  • Have you tried to use parameter --bind_ip_all? What is result if you try to use (shell inside of docker host) 'telnet 30000', do you get any answer? If I know right how docker works, those ports 3000x are connected to all host machines network cards, so it's no difference are you using localhost or host-ip... If both don't work, then there must be some block (like firewall) – JJussi Jun 06 '20 at 18:20
  • Hi @Rajkrishnan, did you solve the problem? I am facing the same issue here. Thanks in advance. – user3792685 Jan 21 '21 at 09:54