GithubHelp home page GithubHelp logo

khinhvuphiduong / devcontainers-mongodb-replica-set-with-docker Goto Github PK

View Code? Open in Web Editor NEW

This project forked from sntnupl/devcontainers-mongodb-replica-set-with-docker

0.0 0.0 0.0 541 KB

Docker based development container template to run a MongoDB replica set cluster in local machine.

Home Page: https://sntnupl.com/mongodb-replicaset-for-development-using-docker

Shell 100.00%

devcontainers-mongodb-replica-set-with-docker's Introduction

Set up a secure MongoDB Replica-set for development using Docker

This repository showcases how one can setup a secure MongoDB replica set for development using Docker.
I have explained the details of the docker-compose.yml file in my blog post, link to which can be found at this link.

NOTE
This replica set is for educational and demonstration purposes ONLY.
Running multiple nodes of a Replica Set within a single machine is an anti-pattern, and MUST BE AVOIDED in Production.
Using this setup for local development though, is perfectly fine.

Getting Started

Most of the stuff here will work out-of-the-box, and the containers will be up and running once you run docker-compose up -d. However, there are a few configuration steps that you would need to incorporate - which are mentioned below.

Configure environmental variables

If you check the docker-compose.yml file, we are relying on .env file to populate the env variables for the compose. A sample.env file is provided here, use it to create your own .env file.

Create keyfiles for MongoDB nodes to authenticate themselves

For this we need to create a keyfile.

In Linux, the following are the commands:

openssl rand -base64 700 > file.key
chmod 400 file.key
sudo chown 999:999 file.key

I have kept a sample file in .docker/mongodb path, but you must follow the steps above to generate a new file.key.

Update hostnames

Once the replica set is up, you will need to update hostnames in local /etc/hosts file.

Sample entry

127.0.0.1 localhost mongo1 mongo2 mongo3

This change is needed to be done in all client machine, from where you would like to connect to this Replica Set.
So if you are running this replica set in your local development machine, and want to connect to it, you would have to update /etc/hosts file in your local dev machine ONLY.
However, if you want to connect to this replica set from a different machine, you would need to update the /etc/hosts file in that other machine as well.

Sample entry on a different machine

10.20.30.40 mongo1 mongo2 mongo3

Here, 10.20.30.40 is the public-ip of the machine where this replica set is running.

NOTE: In windows, the hosts file is located at C:\Windows\System32\drivers\etc\hosts

Result

Once the MongoDB replica set is up and running, Once everything comes up, you can run docker-compose status, and see something like this:

replica set status

To connect to the replica set, you can use mongo client.

Connect to replica set running on local machine

$ mongo "mongodb://localhost:30001,localhost:30002,localhost:30003/<MONGO_INITDB_DATABASE>?replicaSet=rs0" -u <MONGO_INITDB_USERNAME> --authenticationDatabase admin

Connect to local Replica set


Connect to replica set running on local or remote machine

$ mongo "mongodb://mongo1:30001,mongo2:30002,mongo3:30003/<MONGO_INITDB_DATABASE>?replicaSet=rs0" -u <MONGO_INITDB_USERNAME> --authenticationDatabase admin

As mentioned above, you MUST update the local hosts file in the client machine (running this command), to point to the public IP address of the machine where the replica set is running.

Connect to remote Replica set


Remember to replace the <MONGO_INITDB_DATABASE> and <MONGO_INITDB_USERNAME> tags with appropriate values that you've put in the .env file.

Once connected you can use the more succinct db.isMaster() command to debug details like hosts of the replicaset a client must connect to, if the node is primary, etc.

db.isMaster()

Connect to remote Replica set

Full documentation of the command is available at this link.

devcontainers-mongodb-replica-set-with-docker's People

Contributors

sntnupl 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.