title: MongoDB Docker Replica Set - Application Connection Timeout
body: <p>I was successfully was able to create the docker replica set using three containers:</p>
  
  <pre><code>CONTAINER ID        IMAGE               PORTS              NAMES
  b530275d1958        mongo       0.0.0.0:30003-&gt;27017/tcp   mongo3
  dca4fa2d6f93        mongo       0.0.0.0:30002-&gt;27017/tcp   mongo2
  0b4823661cf1        mongo       0.0.0.0:27017-&gt;27017/tcp   mongo1
  </code></pre>
  
  <p>In addition, replica set is successfully configured:</p>
  
  <pre><code>rs0:PRIMARY&gt; 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")
          }
      }
  }
  </code></pre>
  
  <p>It's important to note that it is one VM only connection at ports <code>27017</code> , <code>30002</code> AND <code>30003</code></p>
  
  <p>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</p>
  
  <pre><code>mongodb://&lt;username&gt;:&lt;pass&gt;@172.31.6.177:27017,172.31.6.177:30002,172.31.6.177:30003/opsserver-main?replicaSet=rs0&amp;amp;authSource=admin"
  </code></pre>
  
  <hr>
  
  <p><strong>Error</strong> from my PHP application:</p>
  
  <pre><code>[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']  
  </code></pre>
  
  <p>It is weird to me that it knows the container names , or at least trying to connect to it.  What am i doing wrong?</p>