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

  • Why orientDB connection to localhost of docker is refused
  • Storring and pulling meta-information of a docker image
  • How to change jenkins volume on docker kitematic
  • Windows Container on Windows 10 Insider Build and Docker for Windows
  • Jenkins - Docker integration - Use Jenkins to build Docker images and push to the registry
  • Phoenix Framework - docker image for development and deployment to Kubernetes
  • 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

    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:

        "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("")
    { "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" : "",
                        "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 ''  :
    2016-05-20T01:04:18.012+0000 I NETWORK  [thread1] trying reconnect to ( failed
    2016-05-20T01:04:18.018+0000 I NETWORK  [thread1] reconnect ( ok

    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

  • Flask Rest API based on docker - Graceful shutdown
  • git clone (SSH) gets reaped by supervisord run in docker
  • Exposing Docker Remote API v1.22 on CentOS7
  • Getting error while runnig neo4j-mazerunner project
  • How to connect to containers on docker Overlay network from an external machine
  • Docker: Internal UDP communication
  • 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:

    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 ( 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, 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:

          _id: "rs0",
          version: 1,
          members: [
             { _id: 0, host : "" },
             { _id: 1, host : "" }

    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.