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

**URL:** <https://discuss.circleci.com/t/docker-replica-sets-configuration-how-to-run-rs-initiate/31205>\
**Category:** Build Environment\
**Tags:** mongo\
**Created:** [July 4, 2019, 10:00am UTC](https://discuss.circleci.com/t/docker-replica-sets-configuration-how-to-run-rs-initiate/31205 "2019-07-04T10:00:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hackedieter](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@hackedieter](https://discuss.circleci.com/u/hackedieter)\
**Post date:** [July 4, 2019, 10:00am UTC](https://discuss.circleci.com/t/docker-replica-sets-configuration-how-to-run-rs-initiate/31205/1 "2019-07-04T10:00:38Z")

</div>

[Since this is closed, I can’t ask there.](https://discuss.circleci.com/t/docker-with-command-line-options-mongo-with-replica-set/21057)

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

```auto
  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:

```auto
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

```

---

<div class="post-metadata">

**Author:** ![hackedieter](https://avatars.discourse-cdn.com/v4/letter/h/839c29/32.png) [@hackedieter](https://discuss.circleci.com/u/hackedieter)\
**Post date:** [July 4, 2019, 11:43am UTC](https://discuss.circleci.com/t/docker-replica-sets-configuration-how-to-run-rs-initiate/31205/2 "2019-07-04T11:43:23Z")

</div>

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:

```auto
      - 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

```

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/circleci/original/3X/5/1/511608acda8e85906e15909b57bd0a46c1dbe291.png) [@system](https://discuss.circleci.com/u/system)\
**Post date:** [March 8, 2023, 6:54pm UTC](https://discuss.circleci.com/t/docker-replica-sets-configuration-how-to-run-rs-initiate/31205/7 "2023-03-08T18:54:35Z")

</div>

Closed on March 8, 2023, 6:54pm UTC

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