Skip to content

Via a single command runs a MongoDB Sharded Cluster on a set of Docker containers on your workstation

License

Notifications You must be signed in to change notification settings

adams1mon/sharded-mongodb-docker

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Example MongoDB Sharded Cluster Deployment In Docker Containers

A project requiring a single command to build and run a MongoDB Sharded Cluster in a local workstation 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 workstation, 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 5 seconds.

Prerequisites

  • Your workstation is running a recent version of Linux, Windows or Mac OS X
  • Docker is already installed on your workstation
  • Docker Compose is already installed on your workstation
  • MongoDB Shell is already installed on your workstation to you to issue commands to the running database cluster from your workstation (alternatively use MongoDB Compass graphical tool 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. Connect to the running MongoDB cluster from the MongoDB Shell (the shell will attempt to connect to the first of the two Mongos endpoints):
mongosh --port 27017

sh.status()

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

Tips

  • To show all the running docker containers for this Docker Compose project, run:
sudo docker-compose ps
  • To show the container logs for one of the Mongos routers, 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

Via a single command runs a MongoDB Sharded Cluster on a set of Docker containers on your workstation

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Shell 68.2%
  • Dockerfile 31.8%