├── .gitignore ├── LICENSE ├── README.md ├── docker-compose.yml ├── mongod ├── Dockerfile ├── mongod-runextra.sh ├── mongod-start.sh └── mongod.conf └── mongos ├── Dockerfile ├── mongos-runextra.sh └── mongos-start.sh /.gitignore: -------------------------------------------------------------------------------- 1 | .env 2 | .DS_Store 3 | .vscode 4 | *.log 5 | play.mongodb 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020-2024 Paul Done 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MongoDB Sharded Cluster Deployment Running In Docker Containers 2 | 3 | Provides the ability to execute a single command to build and run a MongoDB Sharded Cluster on a local workstation with each MongoDB component (`mongod`, `mongos`) running in a separate Docker container. Uses a [Docker](https://docs.docker.com/) [Compose](https://docs.docker.com/compose/overview/) project to launch the [sharded MongoDB cluster](https://docs.mongodb.com/manual/sharding/) containerised deployment consisting of 11 separate containers for: 4 | 5 | * 6 `mongod` processes for the 2 shard replica sets 6 | * 3 `mongod` processes for the _configdb_ replica set 7 | * 2 `mongos` router processes 8 | 9 | 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 the localhost forwarded ports 27107 & 27108 , which connect to each of the two mongos processes, respectively. 10 | 11 | The first time you execute the command to build and run the containers, it takes a few minutes to download all the base Docker images. The containers will normally come up in less than 5 seconds when the command is executed the second and subsequent times. 12 | 13 | 14 | ## Prerequisites 15 | 16 | * Your workstation is running a recent version of Linux, Windows or Mac OS X 17 | * [Docker](https://docs.docker.com/install/) is already installed on your workstation 18 | * [Docker Compose](https://docs.docker.com/compose/install/) is already installed on your workstation 19 | * The [MongoDB Shell](https://docs.mongodb.com/mongodb-shell/install/) is already installed on your workstation for to you to issue commands to the running database cluster from your workstation (alternatively use the [MongoDB Compass](https://docs.mongodb.com/compass/current/install/) graphical tool to connect to the cluster) 20 | 21 | _Note, this README assumes you are running version 2+ of Compose (e.g., `docker-compose-v2`) which integrates the `compose` action as an option when running the `docker` command. If you are running '1.x' version of Compose, for the commands shown in this README, swap `docker compose` with `docker-compose` (i.e., introduce a hyphen between the two words, replacing the space)._ 22 | 23 | 24 | ## Build, Run & Connect 25 | 26 | 1. Launch a command line terminal in the base of folder of this project and execute the following command to build and start all the containers in the Docker Compose project: 27 | 28 | ``` 29 | docker compose up --build -d 30 | ``` 31 | 32 | 2. Connect to the running MongoDB cluster from the MongoDB Shell (the shell will attempt to connect to the first of the two `mongos` endpoints) and then issue the command to print the states of the sharded cluster: 33 | 34 | ``` 35 | mongosh --port 27017 36 | ``` 37 | 38 | ``` 39 | sh.status() 40 | ``` 41 | 42 | _Note_: Use port 27018 instead, above, if you want to connect to the second `mongos` endpoint. 43 | 44 | 45 | ## Tips 46 | 47 | * To show all the running docker containers for this Docker Compose project, run: 48 | 49 | ``` 50 | docker compose ps 51 | ``` 52 | 53 | * To show the container logs for one of the `mongos` routers, run: 54 | 55 | ``` 56 | docker compose logs mongos-router0 57 | ``` 58 | 59 | * To execute a terminal session directly in one of the `mongos` containers, to then invoke the MongoDB Shell directly accessing the local `mongos` process, run: 60 | 61 | ``` 62 | docker compose exec mongos-router0 /bin/bash 63 | ``` 64 | 65 | ``` 66 | mongosh 67 | ``` 68 | 69 | * To execute a terminal session directly in one of the `mongod` containers and then view the `mongod` process logs, run: 70 | 71 | ``` 72 | docker compose exec shard0-replica0 /bin/bash 73 | ``` 74 | 75 | ``` 76 | cat /data/db/mongod.log 77 | ``` 78 | 79 | * To shutdown and remove all the Docker Compose project's running containers (ready for you to rebuild and run again), run: 80 | 81 | ``` 82 | docker compose down 83 | ``` 84 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.8' 2 | services: 3 | 4 | ######## MONGODB SHARD0 ######## 5 | 6 | shard0-replica0: 7 | build: 8 | context: mongod 9 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard0 10 | volumes: 11 | - ./mongod/mongod.conf:/etc/mongod.conf 12 | networks: 13 | internalnetwork: 14 | aliases: 15 | - shard0-replica0 16 | environment: 17 | - REPSET_NAME=shard0 18 | - DO_INIT_REPSET=true 19 | expose: 20 | - "27017" 21 | 22 | shard0-replica1: 23 | build: 24 | context: mongod 25 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard0 26 | volumes: 27 | - ./mongod/mongod.conf:/etc/mongod.conf 28 | networks: 29 | internalnetwork: 30 | aliases: 31 | - shard0-replica1 32 | environment: 33 | - REPSET_NAME=shard0 34 | expose: 35 | - "27017" 36 | 37 | shard0-replica2: 38 | build: 39 | context: mongod 40 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard0 41 | volumes: 42 | - ./mongod/mongod.conf:/etc/mongod.conf 43 | networks: 44 | internalnetwork: 45 | aliases: 46 | - shard0-replica2 47 | environment: 48 | - REPSET_NAME=shard0 49 | expose: 50 | - "27017" 51 | 52 | ######## MONGODB SHARD1 ######## 53 | 54 | shard1-replica0: 55 | build: 56 | context: mongod 57 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard1 58 | volumes: 59 | - ./mongod/mongod.conf:/etc/mongod.conf 60 | networks: 61 | internalnetwork: 62 | aliases: 63 | - shard1-replica0 64 | environment: 65 | - REPSET_NAME=shard1 66 | - DO_INIT_REPSET=true 67 | expose: 68 | - "27017" 69 | 70 | shard1-replica1: 71 | build: 72 | context: mongod 73 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard1 74 | volumes: 75 | - ./mongod/mongod.conf:/etc/mongod.conf 76 | networks: 77 | internalnetwork: 78 | aliases: 79 | - shard1-replica1 80 | environment: 81 | - REPSET_NAME=shard1 82 | expose: 83 | - "27017" 84 | 85 | shard1-replica2: 86 | build: 87 | context: mongod 88 | command: mongod -f /etc/mongod.conf --shardsvr --replSet shard1 89 | volumes: 90 | - ./mongod/mongod.conf:/etc/mongod.conf 91 | networks: 92 | internalnetwork: 93 | aliases: 94 | - shard1-replica2 95 | environment: 96 | - REPSET_NAME=shard1 97 | expose: 98 | - "27017" 99 | 100 | ######## MONGODB CONFIGDB ######## 101 | 102 | configdb-replica0: 103 | build: 104 | context: mongod 105 | command: mongod -f /etc/mongod.conf --configsvr --replSet configdb 106 | volumes: 107 | - ./mongod/mongod.conf:/etc/mongod.conf 108 | networks: 109 | internalnetwork: 110 | aliases: 111 | - configdb-replica0 112 | environment: 113 | - REPSET_NAME=configdb 114 | - DO_INIT_REPSET=true 115 | expose: 116 | - "27017" 117 | 118 | configdb-replica1: 119 | build: 120 | context: mongod 121 | command: mongod -f /etc/mongod.conf --configsvr --replSet configdb 122 | volumes: 123 | - ./mongod/mongod.conf:/etc/mongod.conf 124 | networks: 125 | internalnetwork: 126 | aliases: 127 | - configdb-replica1 128 | environment: 129 | - REPSET_NAME=configdb 130 | expose: 131 | - "27017" 132 | 133 | configdb-replica2: 134 | build: 135 | context: mongod 136 | command: mongod -f /etc/mongod.conf --configsvr --replSet configdb 137 | volumes: 138 | - ./mongod/mongod.conf:/etc/mongod.conf 139 | networks: 140 | internalnetwork: 141 | aliases: 142 | - configdb-replica2 143 | environment: 144 | - REPSET_NAME=configdb 145 | expose: 146 | - "27017" 147 | 148 | ######## MONGODB MONGOS ROUTERS ######## 149 | 150 | mongos-router0: 151 | build: 152 | context: mongos 153 | command: mongos --port 27017 --bind_ip 0.0.0.0 --configdb "configdb/configdb-replica0:27017,configdb-replica1:27017,configdb-replica2:27017" 154 | depends_on: 155 | - shard0-replica0 156 | - shard0-replica1 157 | - shard0-replica2 158 | - shard1-replica0 159 | - shard1-replica1 160 | - shard1-replica2 161 | - configdb-replica0 162 | - configdb-replica1 163 | - configdb-replica2 164 | networks: 165 | internalnetwork: 166 | aliases: 167 | - mongos-router0 168 | environment: 169 | - SHARD_LIST=shard0/shard0-replica0:27017,shard0-replica0:27017,shard0-replica0:27017;shard1/shard1-replica0:27017,shard1-replica0:27017,shard1-replica0:27017 170 | expose: 171 | - "27017" 172 | ports: 173 | - "27017:27017" # Map the host workstation's port 27017 to the docker instance exposed port 27017 for 'direct' mongos access 174 | 175 | mongos-router1: 176 | build: 177 | context: mongos 178 | command: mongos --port 27017 --bind_ip 0.0.0.0 --configdb "configdb/configdb-replica0:27017,configdb-replica1:27017,configdb-replica2:27017" 179 | networks: 180 | internalnetwork: 181 | aliases: 182 | - mongos-router1 183 | expose: 184 | - "27017" 185 | ports: 186 | - "27018:27017" # Map the host workstation's port 27018 to the docker instance exposed port 27017 for 'direct' mongos access 187 | 188 | networks: 189 | internalnetwork: {} 190 | -------------------------------------------------------------------------------- /mongod/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM mongo:latest 2 | 3 | RUN apt-get update && apt-get -q install -y 4 | 5 | COPY --chown=mongodb:mongodb mongod-start.sh /usr/local/bin/ 6 | COPY --chown=mongodb:mongodb mongod-runextra.sh /usr/local/bin/ 7 | 8 | RUN chmod u+x /usr/local/bin/mongod-start.sh /usr/local/bin/mongod-runextra.sh 9 | 10 | ENTRYPOINT ["mongod-start.sh"] 11 | 12 | CMD mongod -f /etc/mongod.conf 13 | -------------------------------------------------------------------------------- /mongod/mongod-runextra.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | if [ "$DO_INIT_REPSET" = true ] ; then 4 | # Wait until local MongoDB instance is up and running 5 | until /usr/bin/mongosh --port 27017 --quiet --eval 'db.getMongo()'; do 6 | sleep 1 7 | done 8 | 9 | # Configure a MongoDB replica set if this replica has been asked to 10 | /usr/bin/mongosh --port 27017 <