Docker replica sets configuration - How to run `rs.initiate()`?

Since this is closed, I can’t ask there.

How is this actually achieved to configure the replica set. Do I have to run a setup script, which connects to the instance and runs the rs.initiate() command?

So I added

  mongo_container: &mongo_container
    image: circleci/mongo:4.0.5
    command: [mongod, --bind_ip_all, --replSet, rs0]

But how can I run the configuration?

Thanks!

This is the Error, which looks like just the config is missing:

No primary server is available in cluster: #<Cluster topology=ReplicaSetNoPrimary[localhost:27017,name=rs0] servers=[#<Server address=localhost:27017 GHOST>]> with timeout=30, LT=0.015

Anyone looking for the same: Solved it!

Since the run steps are run on the first container (which was my ruby/test container), you need to install mongo cli tools and run the actual config step as script:

      - run: |
          sudo apt-get install -y mongodb
          mongo --host localhost:27017 \<<EOF
            var cfg = {
                "_id": "rs0",
                "version": 1,
                "members": [
                    {
                        "_id": 0,
                        "host": "localhost:27017",
                        "priority": 2
                    }
                ]
            };
            rs.initiate(cfg, { force: true });
            rs.reconfig(cfg, { force: true });
            db.getMongo().setReadPref('nearest');
          EOF
1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.