What is the proper way of setting a mongodb replica set using docker and fig?
Asked Answered
N

2

8

What is the proper way of setting a mongodb replica set using docker and fig?

I was trying to follow official mongodb tutorials to create a fig.yml file with some replica sets but always got blocked by how to call rs.initiate() and rs.add("<hostname><:port>") properly.

I found this SO answer explaining why I can't start everything just from the shell, without calling rs.initiate(), so how can I accomplish that?

Oh, and I am using mongo:latest (v2.6.5) as base image, without any modifications.

Neolatin answered 12/12, 2014 at 17:50 Comment(0)
R
3

I had a similar problem, this is what I did. I'm using docker-compose instead of fig.

In my docker-compose.

mongors:                                                                                                
  image: mongo                                                                                          
  ports:                                                                                                
    - "27017:27017"                                                                                     
  volumes:                                                                                              
   - ./mongo:mongo                                                                                      
  entrypoint: mongo/entrypoint.sh

In my entrypoint.sh:

#!/bin/bash
mongod --port 27018 --replSet rs0 --fork --syslog --smallfiles
mongo --port 27018 --eval "rs.initiate({_id : 'rs0', members : [{_id : 0, host : 'localhost:27018'}]})"
mongo --port 27018 --eval "while(true) {if (rs.status().ok) break;sleep(1000)};"

Make sure it's executable:

chmod +x mongo/entrypoint.sh

It's a little hacky, but it works :)

Raynard answered 29/6, 2015 at 14:5 Comment(0)
M
-4

First Stop the mongod service, before setting the replica set

 service mongodb stop

mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"

 mongod --port 27017 --dbpath "D:\set up\mongodb\data" --replSet rs0 --fork

It will start a mongod instance with the name rs0, on port 27017. Now start the command prompt and connect to this mongod instance.

In mongo client issue the command rs.initiate() to initiate a new replica set.

 rs.initiate()

To check the replica set configuration issue the command rs.conf().

rs.conf() should have the following

{
"_id" : "rs0"
"version" : 1,
"members" : [
    {
        "_id" : 0,
        "host" "localhost:27017"
    },
    {
        "_id" : 1,
        "host" "localhost:27018"
    },
    {
        "_id" : 2,
        "host" "localhost:27019"
    }
]
}

Now, you can add the additional nodes to the replication set by referencing the hostname you gave them.

 rs.add("localhost:27019")
 { "ok" : 1 }

Do this for each of your remaining replication members. Your replication set should now be up and running.

To check the status of replica set issue the command rs.status().

rs.status()

Hope this helps.

Mamiemamma answered 18/12, 2014 at 8:4 Comment(2)
This is not about setting up a replica set on docker/fig, this is about setting it is up in general in a test environment.Tommie
Yes, and also automating the process... the whole point is to start fig or the docker image and not worry about entering more commands or checking status... You just described the link I already had posted in the questionNeolatin

© 2022 - 2024 — McMap. All rights reserved.