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.
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 :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With