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.
First Stop the mongod service, before setting the replica set
mongod --port "PORT" --dbpath "YOUR_DB_DATA_PATH" --replSet "REPLICA_SET_INSTANCE_NAME"
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.
To check the replica set configuration issue the command rs.conf().
rs.conf() should have the following
Now, you can add the additional nodes to the replication set by referencing the hostname you gave them.
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().
Hope this helps.
I had a similar problem, this is what I did. I'm using docker-compose instead of fig.
In my docker-compose.
In my entrypoint.sh:
Make sure it's executable:
It's a little hacky, but it works :)