Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 819
Felipe Sabino Avatar asked Dec 12 '14 17:12

Felipe Sabino


1 Answers

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 :)

like image 64
michael_erasmus Avatar answered Oct 04 '22 02:10

michael_erasmus