title: Connection error for MongoDb replica-set using docker-compose
body: <p>I'm trying to connect to mongodb replica set running under 4 containers: 3 mongo as a cluster, and 1 to issue mongo commands to others.</p>
  
  <p>Here I don't know how should I connect it to my application with a mongodb url.</p>
  
  <p><code>docker-compose.yml</code> is</p>
  
  <pre><code>mongo3:
    image: mongo:latest
    hostname: mongo3
    expose:
      - "27017"
      - "28017"
    restart: always
  
        entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
  
  mongo2:
    image: mongo:latest
    hostname: mongo2
    expose:
      - "27017"
      - "28017"
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
  
  mongo1:
    image: mongo:latest
    hostname: mongo1
    expose:
      - "27017"
      - "28017"
    links:
      - mongo2:mongo2
      - mongo3:mongo3
    restart: always
    entrypoint: [ "/usr/bin/mongod", "--replSet", "rs"]
  
  mongosetup:
    image: mongo:latest
    links:
      - mongo1:mongo1
      - mongo2:mongo2
      - mongo3:mongo3
    volumes:
      - ./scripts:/scripts
      - ./data/runtime/db:/data/db
      - ./data/dump:/dump
  
    restart: always
    entrypoint: [ "bash", "/scripts/mongo_setup.sh" ]
  
  app:
    image: papagari/swoop.chat:latest
    environment:
      - MONGO_URL=mongodb://mongo1:27017,mongo2:27017,mongo3:27017/swoop?replicaSet=rs
      - ROOT_URL=https://localhost:3000
    links:
      - mongo1:mongo1
      - mongo2:mongo2
      - mongo3:mongo3
    ports:
      - 3000:3000
  </code></pre>
  
  <p>and <code>/scripts/mongo_setup.sh</code> is</p>
  
  <pre><code>MONGODB1=`ping -c 1 mongo1 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
  MONGODB2=`ping -c 1 mongo2 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`
  MONGODB3=`ping -c 1 mongo3 | head -1  | cut -d "(" -f 2 | cut -d ")" -f 1`    
  
  echo "Waiting for startup.."
  until curl http://${MONGODB1}:28017/serverStatus\?text\=1 2&gt;&amp;1 | grep uptime | head -1; do
    printf '.'
    sleep 1
  done    
  
  echo curl http://${MONGODB1}:28017/serverStatus\?text\=1 2&gt;&amp;1 | grep uptime | head -1
  echo "Started.."    
  
  sleep 10    
  
  echo SETUP.sh time now: `date +"%T" `
  mongo --host ${MONGODB1}:27017 &lt;&lt;EOF
     var cfg = {
          "_id": "rs",
          "version": 1,
          "members": [
              {
                  "_id": 0,
                  "host": "${MONGODB1}:27017",
                  "priority": 2
              },
              {
                  "_id": 1,
                  "host": "${MONGODB2}:27017",
                  "priority": 0
              },
              {
                  "_id": 2,
                  "host": "${MONGODB3}:27017",
                  "priority": 0
              }
          ]
      };
      rs.initiate(cfg, { force: true });
      rs.reconfig(cfg, { force: true });
      db.getMongo().setReadPref('nearest');
  EOF
  </code></pre>
  
  <p>Now I'm getting an error for <code>mongodb://mongo1:27017,mongo2:27017,mongo3:27017/swoop?replicaSet=rs</code> 
  Obviously <code>failed to connect to server</code> because the url I've used is not valid. I'm new to docker and mongo db replica set for the first time, trying to understand docker here. How it should work.   </p>
  
  <p>Error: <code>MongoError: no primary found in replicaset</code>.</p>
  
  <p>Complete Error: </p>
  
  <pre><code>app_1        | 
  app_1        | /app/bundle/programs/server/node_modules/fibers/future.js:313
  app_1        |                      throw(ex);
  app_1        |                      ^
  app_1        | MongoError: no primary found in replicaset
  app_1        |     at Object.Future.wait (/app/bundle/programs/server/node_modules/fibers/future.js:449:15)
  app_1        |     at new MongoConnection (packages/mongo/mongo_driver.js:216:27)
  app_1        |     at new MongoInternals.RemoteCollectionDriver (packages/mongo/remote_collection_driver.js:4:16)
  app_1        |     at Object.&lt;anonymous&gt; (packages/mongo/remote_collection_driver.js:38:10)
  app_1        |     at Object.defaultRemoteCollectionDriver (packages/underscore/underscore.js:750:1)
  app_1        |     at new Mongo.Collection (packages/mongo/collection.js:103:40)
  app_1        |     at AccountsServer.AccountsCommon (packages/accounts-base/accounts_common.js:23:18)
  app_1        |     at new AccountsServer (packages/accounts-base/accounts_server.js:18:5)
  app_1        |     at meteorInstall.node_modules.meteor.accounts-base.server_main.js (packages/accounts-base/server_main.js:9:12)
  app_1        |     at fileEvaluate (packages/modules-runtime/.npm/package/node_modules/install/install.js:153:1)
  app_1        |     - - - - -
  app_1        |     at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/replset.js:631:32
  app_1        |     at [object Object].&lt;anonymous&gt; (/app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/replset.js:421:24)
  app_1        |     at [object Object].g (events.js:260:16)
  app_1        |     at emitOne (events.js:77:13)
  app_1        |     at [object Object].emit (events.js:169:7)
  app_1        |     at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/topologies/server.js:283:14
  app_1        |     at /app/bundle/programs/server/npm/node_modules/meteor/npm-mongo/node_modules/mongodb-core/lib/connection/pool.js:436:18
  app_1        |     at nextTickCallbackWith0Args (node.js:420:9)
  app_1        |     at process._tickCallback (node.js:349:13)
  </code></pre>
  
  <p>I've searched many placed all I got is with local machine or the server machine not with docker links.</p>
  
  <p><strong>Update</strong>
  response of <code>rs.status()</code> in mongo1 container, which look pretty fine, primary and secondary are initialized properly.</p>
  
  <pre><code>{
      "set" : "rs",
      "date" : ISODate("2016-10-19T06:31:10.861Z"),
      "myState" : 1,
      "term" : NumberLong(1),
      "heartbeatIntervalMillis" : NumberLong(2000),
      "members" : [
          {
              "_id" : 0,
              "name" : "172.17.0.4:27017",
              "health" : 1,
              "state" : 1,
              "stateStr" : "PRIMARY",
              "uptime" : 62,
              "optime" : {
                  "ts" : Timestamp(1476858630, 1),
                  "t" : NumberLong(1)
              },
              "optimeDate" : ISODate("2016-10-19T06:30:30Z"),
              "infoMessage" : "could not find member to sync from",
              "electionTime" : Timestamp(1476858629, 1),
              "electionDate" : ISODate("2016-10-19T06:30:29Z"),
              "configVersion" : 13918,
              "self" : true
          },
          {
              "_id" : 1,
              "name" : "172.17.0.3:27017",
              "health" : 1,
              "state" : 2,
              "stateStr" : "SECONDARY",
              "uptime" : 51,
              "optime" : {
                  "ts" : Timestamp(1476858630, 1),
                  "t" : NumberLong(1)
              },
              "optimeDate" : ISODate("2016-10-19T06:30:30Z"),
              "lastHeartbeat" : ISODate("2016-10-19T06:31:09.633Z"),
              "lastHeartbeatRecv" : ISODate("2016-10-19T06:31:10.291Z"),
              "pingMs" : NumberLong(0),
              "syncingTo" : "172.17.0.2:27017",
              "configVersion" : 13918
          },
          {
              "_id" : 2,
              "name" : "172.17.0.2:27017",
              "health" : 1,
              "state" : 2,
              "stateStr" : "SECONDARY",
              "uptime" : 51,
              "optime" : {
                  "ts" : Timestamp(1476858630, 1),
                  "t" : NumberLong(1)
              },
              "optimeDate" : ISODate("2016-10-19T06:30:30Z"),
              "lastHeartbeat" : ISODate("2016-10-19T06:31:09.633Z"),
              "lastHeartbeatRecv" : ISODate("2016-10-19T06:31:09.283Z"),
              "pingMs" : NumberLong(0),
              "syncingTo" : "172.17.0.4:27017",
              "configVersion" : 13918
          }
      ],
      "ok" : 1
  }
  </code></pre>
  
  <p>Any tips will be helpful. TIA</p>