Reputation: 1137
I was successfully was able to create the docker replica set using three containers:
CONTAINER ID IMAGE PORTS NAMES
b530275d1958 mongo 0.0.0.0:30003->27017/tcp mongo3
dca4fa2d6f93 mongo 0.0.0.0:30002->27017/tcp mongo2
0b4823661cf1 mongo 0.0.0.0:27017->27017/tcp mongo1
In addition, replica set is successfully configured:
rs0:PRIMARY> rs.status()
{
"set" : "rs0",
"date" : ISODate("2018-01-08T20:57:30.395Z"),
"myState" : 1,
"term" : NumberLong(16),
"heartbeatIntervalMillis" : NumberLong(2000),
"optimes" : {
"lastCommittedOpTime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"readConcernMajorityOpTime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"appliedOpTime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"durableOpTime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
}
},
"members" : [
{
"_id" : 0,
"name" : "mongo1:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 898,
"optime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"optimeDate" : ISODate("2018-01-08T20:57:26Z"),
"electionTime" : Timestamp(1515444174, 1),
"electionDate" : ISODate("2018-01-08T20:42:54Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "mongo2:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 877,
"optime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"optimeDurable" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"optimeDate" : ISODate("2018-01-08T20:57:26Z"),
"optimeDurableDate" : ISODate("2018-01-08T20:57:26Z"),
"lastHeartbeat" : ISODate("2018-01-08T20:57:29.056Z"),
"lastHeartbeatRecv" : ISODate("2018-01-08T20:57:30.324Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo1:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "mongo3:27017",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 871,
"optime" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"optimeDurable" : {
"ts" : Timestamp(1515445046, 1),
"t" : NumberLong(16)
},
"optimeDate" : ISODate("2018-01-08T20:57:26Z"),
"optimeDurableDate" : ISODate("2018-01-08T20:57:26Z"),
"lastHeartbeat" : ISODate("2018-01-08T20:57:29.055Z"),
"lastHeartbeatRecv" : ISODate("2018-01-08T20:57:29.506Z"),
"pingMs" : NumberLong(0),
"syncingTo" : "mongo1:27017",
"configVersion" : 1
}
],
"ok" : 1,
"operationTime" : Timestamp(1515445046, 1),
"$clusterTime" : {
"clusterTime" : Timestamp(1515445046, 1),
"signature" : {
"hash" : BinData(0,"c3aBSVWElstPhnBx3c5NysBfdmk="),
"keyId" : NumberLong("6504664228182360065")
}
}
}
It's important to note that it is one VM only connection at ports 27017
, 30002
AND 30003
Yet I seem to be having an issue when my PHP application connects to it using the mongo DSN string that I attach it to
mongodb://<username>:<pass>@172.31.6.177:27017,172.31.6.177:30002,172.31.6.177:30003/opsserver-main?replicaSet=rs0&authSource=admin"
Error from my PHP application:
[Doctrine\MongoDB\Exception\ResultException]
No suitable servers found: `serverSelectionTimeoutMS` expired: [connection timeout calling i
smaster on '172.31.6.177:27017'] [connection timeout calling ismaster on 'mongo1:27017'] [co
nnection timeout calling ismaster on 'mongo2:27017'] [connection timeout calling ismaster on
'mongo3:27017']
It is weird to me that it knows the container names , or at least trying to connect to it. What am i doing wrong?
Upvotes: 2
Views: 1290
Reputation: 13765
This is because inside the docker network, the containers mongo1
, mongo2
, and mongo3
can find each other. However, those names are not known by the host system, so your application cannot connect to the whole set.
You can try pinging mongo1
from your host system, where it should say Unknown host
.
The easiest way to solve this is to change the hostnames in your replica set config (the hostnames you use when initiating the replica set with rs.initiate(...)
), e.g.:
mongo1:27017
--> <your local hostname>:27017
mongo2:27017
--> <your local hostname>:30002
mongo3:27017
--> <your local hostname>:30003
Replace <your local hostname>
with the output of the hostname -f
command.
This way, your local host can find all your replica set nodes, and all your replica set nodes can still find each other via your local host's port mappings.
Upvotes: 3