Giter VIP home page Giter VIP logo

docker-compose-mongo-replica-set's Introduction

Running a Local Mongo Replica Set

Contents

Disclaimer

โš ๏ธ This setup is purely for local development purposes.

This setup should not be used for production applications as it was not built with that in mind.

What is it?

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

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

Versions

  • Different versions are available to cater for the various major versions of Mongo
  • You can find the most suitable one by looking at the tags or releases

Are there any prerequisites?

  • Docker
  • Docker Compose
  • 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

Connecting with URI

mongodb://mongo1:30001,mongo2:30002,mongo3:30003/?replicaSet=my-replica-set

Thanks / Further Reading

docker-compose-mongo-replica-set's People

Contributors

ma3574 avatar sfmohassel avatar sidzan avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.