replica set in MongoDB using docker, primary has error and stops being primary when another member is added to the set

I have two docker containers running a mongo instance each, they were initialized like this:

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 

when i do docker inspect mongodb-shard-1-node-1 it shows the ip 172.17.0.2

  • docker-compose up Permission denied on Windows
  • Crash during startup with network share for gitlab container
  • Where to see the logs for a failed deployment on managed Vms on Google App Engine?
  • Running commands inside Docker Container
  • How to properly setup a data volume container?
  • REST request across networks
  • 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 
    

    when i do docker inspect mongodb-shard-1-node-2 it shows the ip 172.17.0.4

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

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

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

    rs0:PRIMARY> rs.add("172.17.0.4:27017")
    { "ok" : 1 }
    rs0:PRIMARY> 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
    }
    

    but right away it fails for some reason and i have no idea why, here’s what i get:

    rs0:PRIMARY> 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
    

    What is wrong? How do I fix it?

    Edit: just to clarify, i had already tried the connections between the containers by doing what this part of the documentation says at: Test Connections Between all Members

  • How do I start cron on docker ubuntu base?
  • How to access database which runs on host from inside of container?
  • How to install and run postgress using docker-compose.yml version “1”
  • Import AMI image as Docker image
  • Docker container does not give me a shell
  • Bitbucket pipelines: link cakephp image with mysql image together
  • 2 Solutions collect form web for “replica set in MongoDB using docker, primary has error and stops being primary when another member is added to the set”

    Had my question answered here:
    https://dba.stackexchange.com/a/139145/91866

    I’m gonna quote the whole answer:

    Your primary is trying to auto-configure itself as ee3c41ef76b2:27017 and that then resolves to the loopback (127.0.0.1) which is then not responding on the container as it expects. Depending on what the second container does to resolve ee3c41ef76b2, and especially it it does not resolve to 172.17.0.2, it will probably not be able to talk to the primary either.

    Assuming you are correct about the connectivity (and you have verified that the instances are listening on the IP and not just the loopbasck) then you need to override the automatic detection and be explicit when you are calling rs.initiate(), something like this:

    rs.initiate(
        {
          _id: "rs0",
          version: 1,
          members: [
             { _id: 0, host : "172.17.0.2:27017" },
             { _id: 1, host : "172.17.0.4:27017" }
          ]
        }
    )
    

    I too am having this very problem. using the latest version of the docker image v3.2.6, on docker 1.11.1

    Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.