replica set in MongoDB using docker, primary has error and stops being primary when another member is added to the set
<p>I have two docker containers running a mongo instance each, they were initialized like this:</p>

<pre><code>docker run --name mongodb-shard-1-node-1 -d -v mongodb-shard-1-node-1:/data/db -p 27031:27017 mongo --replSet rs0 --smallfiles --oplogSize 128 
</code></pre>

<p>when i do <code>docker inspect mongodb-shard-1-node-1</code> it shows the ip 172.17.0.2</p>

<pre><code>docker run --name mongodb-shard-1-node-2 -d -v mongodb-shard-1-node-2:/data/db -p 27020:27017 mongo --replSet rs0 --smallfiles --oplogSize 128 
</code></pre>

<p>when i do <code>docker inspect mongodb-shard-1-node-2</code> it shows the ip 172.17.0.4</p>

<p>So i proceed to access <code>mongodb-shard-1-node-1</code> by using <code>docker exec -it mongodb-shard-1-node-1 mongo</code> and i initialize it as the primary member like this:</p>

<pre><code>rs.initiate()
{
    "info2" : "no configuration specified. Using a default configuration for the set",
    "me" : "ee3c41ef76b2:27017",
    "ok" : 1
}
</code></pre>

<p>Then I proceed to add the <code>mongodb-shard-1-node-2</code> to this member for it to work as a secondary member, at first it looks like it worked: </p>

<pre><code>rs0:PRIMARY&gt; rs.add("172.17.0.4:27017")
{ "ok" : 1 }
rs0:PRIMARY&gt; rs.status()
{
    "set" : "rs0",
    "date" : ISODate("2016-05-20T01:04:02.095Z"),
    "myState" : 1,
    "term" : NumberLong(1),
    "members" : [ervalMillis" : NumberLong(2000),
            {       "_id" : 0,
                    "name" : "ee3c41ef76b2:27017",
                    "state" : 1,,
                    "uptime" : 27,PRIMARY",
                    "optime""ts" : Timestamp(1463706237, 1),
                    },      "t" : NumberLong(1)
                    "infoMessage" : "could not find member to sync from",
                    "electionDate" : ISODate("2016-05-20T01:03:43Z"),
                    "self" : truen" : 2,
            {,
                    "name" : "172.17.0.4:27017",
                    "state" : 0,,
                    "uptime" : 4,"STARTUP",
                    "optime""ts" : Timestamp(0, 0),
                    },      "t" : NumberLong(-1)
                    "lastHeartbeat" : ISODate("2016-05-20T01:04:01.187Z"),
                    "pingMs" : NumberLong(0),Date("1970-01-01T00:00:00Z"),
            }       "configVersion" : -2
    "ok" : 1
}
</code></pre>

<p>but right away it fails for some reason and i have no idea why, here's what i get: </p>

<pre><code>rs0:PRIMARY&gt; rs.status()
2016-05-20T01:04:18.007+0000 E QUERY    [thread1] Error: error doing query:
failed: network error while attempting to run command 'replSetGetStatus' on host '127.0.0.1:27017'  :
DB.prototype.runCommand@src/mongo/shell/db.js:135:1
DB.prototype.adminCommand@src/mongo/shell/db.js:153:16
rs.status@src/mongo/shell/utils.js:1090:12
@(shell):1:1

2016-05-20T01:04:18.012+0000 I NETWORK  [thread1] trying reconnect to 127.0.0.1:27017 (127.0.0.1) failed
2016-05-20T01:04:18.018+0000 I NETWORK  [thread1] reconnect 127.0.0.1:27017 (127.0.0.1) ok
rs0:SECONDARY
</code></pre>

<p>What is wrong? How do I fix it?</p>

<p><strong>Edit</strong>: just to clarify, i had already tried the connections between the containers by doing what <a href="https://docs.mongodb.com/v3.0/tutorial/troubleshoot-replica-sets/" rel="nofollow">this part</a> of the documentation says at: Test Connections Between all Members</p>