andris9 / sharded-mongodb-docker

Runs a MongoDB Sharded Cluster on a set of Docker containers on your laptop/PC from a single command

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Example Containerised MongoDB Sharded Cluster Project

A project requiring a single command to build and run a MongoDB Sharded Cluster in a local laptop/PC environment with each MongoDB component (Mongod, Mongos) running in a separate Docker container. Uses a Docker Compose project to launch the sharded MongoDB cluster containerised deployment.

Each element in the deployment topology (9 Mongod processes for the 2 shard replica sets and the configdb replica set + 2 Mongos processes) runs in its own Docker container and all the containers are visible to each other on the same internal network. Once running, the MongoDB cluster is accessible directly from your laptop/PC, via localhost forwarded ports 27107 & 27108 which connect to each of the two Mongos processes respectively.

The first time you execute the command to build and run the containers, it take a couple of minutes to download all the base Docker images. When executed the second and subsequent times, the containers will all come up in around 10 seconds.

Prerequisites

  • Your laptop/PC is running a recent version of Linux, Windows or Mac OS X
  • Docker is already installed on your laptop/PC
  • Docker Compose is already installed on your laptop/PC
  • MongoDB is already installed on your laptop/PC to enable the Mongo Shell to be run directly from your laptop/PC (alternatively use MongoDB Compass to connect to the cluster)

Build, Run & Connect

  1. Launch a command line terminal in the base sharded-mongodb-docker folder and execute the following command to build and start all the containers in the Docker Compose project:
sudo docker-compose up --build -d
  1. Show all the running docker containers for this Docker Compose project:
sudo docker-compose ps
  1. Connect to the MongoDB cluster from the Mongo Shell (the Shell should connect to the first of the two Mongos endpoints):
mongo --port 27017
sh.status()
exit

Note: Use port 27018 instead, above, if you want to connect to the second Mongos.

Tips

  • To show the container logs for one of the Mongos servers, run:
sudo docker-compose logs mongos-router0
  • To execute a terminal session directly in one of the Mongos containers and then execute the Mongo Shell directly accessing the local Mongos process, run:
sudo docker-compose exec mongos-router0 /bin/bash
mongo
  • To execute a terminal session directly in one of the Mongod containers and then view the Mongod process' logs, run:
sudo docker-compose exec shard0-replica0 /bin/bash
cat /data/db/mongod.log
  • To shutdown and remove all the Docker Compose project's running containers (ready for you to rebuild and run again), run:
sudo docker-compose down

About

Runs a MongoDB Sharded Cluster on a set of Docker containers on your laptop/PC from a single command

License:MIT License


Languages

Language:Shell 68.1%Language:Dockerfile 31.9%