Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mongo replica node with docker

I have setup a Mongo Primary node with docker using docker image https://hub.docker.com/_/mongo/. Now I wish to add replica node in my current setup using docker only. Can anybody help me with this?

like image 426
Harshad Yeola Avatar asked Dec 29 '25 08:12

Harshad Yeola


1 Answers

You could check a guide like "Deploy a MongoDB Cluster in 9 steps Using Docker", but that involve multiple working docker servers.

It uses a key file key file to be used on all nodes (openssl rand -base64 741 > mongodb-keyfile) for internal authentication.
That allows to create an admin user, which then adds the replica servers:

docker run \
--name mongo \
-v /home/core/mongo-files/data:/data/db \
-v /home/core/mongo-files:/opt/keyfile \
--hostname="node1.example.com" \
--add-host node1.example.com:${node1} \
--add-host node2.example.com:${node2} \
--add-host node3.example.com:${node3} \
-p 27017:27017 -d mongo:2.6.5 \
--smallfiles \
--keyFile /opt/keyfile/mongodb-keyfile \
--replSet "rs0"

On each replica, you initiate and then check the config:

 rs.initiate()
 rs.conf()

Back to node 1, you declare the replica:

rs0:PRIMARY> rs.add("node2.example.com")
rs0:PRIMARY> rs.add("node3.example.com")
like image 121
VonC Avatar answered Jan 01 '26 01:01

VonC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!