Skip to content

A way to run a Mongo replica set locally using Docker with minimal configuration required.

License

Notifications You must be signed in to change notification settings

hshar7/docker-compose-mongo-replica-set

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running a Local Mongo Replica Set

Contents

What is it?

This docker-compose setup starts a local mongo replica set with 3 instances running on:

  • mongo1:30001
  • mongo2:30002
  • mongo3:30003

Are there any prerequisites?

• Docker Compose • Docker • The following in your /etc/hosts file:

127.0.0.1       mongo1
127.0.0.1       mongo2
127.0.0.1       mongo3

How do I run the Replica Set?

Simples:

docker-compose up -d

How do I access the Mongo Shells for each Instance?

docker exec -it mongo1 sh -c "mongo --port 30001"
docker exec -it mongo2 sh -c "mongo --port 30002"
docker exec -it mongo3 sh -c "mongo --port 30003"

How does it work?

  • Starts three instances of Mongo
  • On the first instance it runs the following Mongo Shell command:
rs.initiate(
  {
    _id : 'my-replica-set',
    members: [
      { _id : 0, host : "mongo1:30001" },
      { _id : 1, host : "mongo2:30002" },
      { _id : 2, host : "mongo3:30003" }
    ]
  }
)
  • This causes all 3 instances to join the replica set named my-replica-set and start talking to each other
  • One is elected to become the PRIMARY and the other two become SECONDARY instances
  • The Docker healthcheck config is used to cause the initialisation of the replica set. More info in the further reading links.

Robo 3T

I used Robo 3T to test it locally and used the following config for the connection:

Robo 3T Config

Thanks / Further Reading

About

A way to run a Mongo replica set locally using Docker with minimal configuration required.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published