Hi everyone! I’m new and in these days I tried to set up a project I found on GitHub to try to better understand how to set up MongoDB through docker. So, I installed MongoDB on MacOS and this is the docker compose file in the directory:
version: ‘3’
services:
# replica set 1
mongors1n1:
container_name: mongors1n1
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27017:27017
expose:
- “27017”
volumes:
- ~/mongo_cluster/data1:/data/db
mongors1n2:
container_name: mongors1n2
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27027:27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/data2:/data/db
mongors1n3:
container_name: mongors1n3
image: mongo
command: mongod --shardsvr --replSet mongors1 --dbpath /data/db --port 27017
ports:
- 27037:27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/data3:/data/db
# replica set 2
mongors2n1:
container_name: mongors2n1
image: mongo
command: mongod --shardsvr --replSet mongors2 --dbpath /data/db --port 27017
ports:
- 27047:27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/data4:/data/db
mongors2n2:
container_name: mongors2n2
image: mongo
command: mongod --shardsvr --replSet mongors2 --dbpath /data/db --port 27017
ports:
- 27057:27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/data5:/data/db
mongors2n3:
container_name: mongors2n3
image: mongo
command: mongod --shardsvr --replSet mongors2 --dbpath /data/db --port 27017
ports:
- 27067:27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/data6:/data/db
# mongo config server
mongocfg1:
container_name: mongocfg1
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/config1:/data/db
mongocfg2:
container_name: mongocfg2
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/config2:/data/db
mongocfg3:
container_name: mongocfg3
image: mongo
command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
expose:
- "27017"
volumes:
- ~/mongo_cluster/config3:/data/db
# mongos router
mongos1:
container_name: mongos1
image: mongo
depends_on:
- mongocfg1
- mongocfg2
command: mongos --configdb mongors1conf/mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
ports:
- 27019:27017
expose:
- "27017"
mongos2:
container_name: mongos2
image: mongo
depends_on:
- mongocfg1
- mongocfg2
command: mongos --configdb mongors1conf/mongocfg1:27017,mongocfg2:27017,mongocfg3:27017 --port 27017
ports:
- 27020:27017
expose:
- "27017"
# TODO after running docker-compose
# conf = rs.config();
# conf.members[0].priority = 2;
# rs.reconfig(conf);
And this is the script to run and create the shards etc…:
#!/bin/sh
docker-compose up
# configure our config servers replica set
docker exec -it mongocfg1 bash -c “echo ‘rs.initiate({_id: "mongors1conf",configsvr: true, members: [{ _id : 0, host : "mongocfg1" },{ _id : 1, host : "mongocfg2" }, { _id : 2, host : "mongocfg3" }]})’ | mongo”
# building replica shard
docker exec -it mongors1n1 bash -c "echo 'rs.initiate({_id : \"mongors1\", members: [{ _id : 0, host : \"mongors1n1\" },{ _id : 1, host : \"mongors1n2\" },{ _id : 2, host : \"mongors1n3\" }]})' | mongo"
docker exec -it mongors2n1 bash -c "echo 'rs.initiate({_id : \"mongors2\", members: [{ _id : 0, host : \"mongors2n1\" },{ _id : 1, host : \"mongors2n2\" },{ _id : 2, host : \"mongors2n3\" }]})' | mongo"
# we add shard to the routers
docker exec -it mongos1 bash -c "echo 'sh.addShard(\"mongors1/mongors1n1\")' | mongo "
docker exec -it mongos1 bash -c "echo 'sh.addShard(\"mongors2/mongors2n1\")' | mongo "
If I try to run directly the script I get the errors:
mongos1 | {“t”:{“$date”:“2021-07-25T09:03:56.101+00:00”},“s”:“I”, “c”:“-”, “id”:4333222, “ctx”:“ReplicaSetMonitor-TaskExecutor”,“msg”:“RSM received error response”,“attr”:{“host”:“mongocfg3:27017”,“error”:“HostUnreachable: Error connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by :: Connection refused”,“replicaSet”:“mongors1conf”,“response”:“{}”}}
mongos1 | {“t”:{“$date”:“2021-07-25T09:03:56.101+00:00”},“s”:“I”, “c”:“NETWORK”, “id”:4712102, “ctx”:“ReplicaSetMonitor-TaskExecutor”,“msg”:“Host failed in replica set”,“attr”:{“replicaSet”:“mongors1conf”,“host”:“mongocfg3:27017”,“error”:{“code”:6,“codeName”:“HostUnreachable”,“errmsg”:“Error connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by :: Connection refused”},“action”:{“dropConnections”:true,“requestImmediateCheck”:false,“outcome”:{“host”:“mongocfg3:27017”,“success”:false,“errorMessage”:“HostUnreachable: Error connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by :: Connection refused”}}}}
And a list of errors I copied here: link
Shouldn’t docker configure all the files without the user has to? Or do I need to create something manually like the database etc.?