title: First time with MongoDB + Docker - Set up from docker compose
body: <p>I'd like to try a <a href="https://github.com/valentino-dicianni/MAADB-ProjectMeo" rel="nofollow noreferrer">project</a> I found on GitHub, so I installed MongoDB on MacOS and now I'm trying to understand how to set up it correctly through the docker compose file in the directory. This is the docker file:</p>
  <pre><code>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:
        - &quot;27017&quot;
      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:
        - &quot;27017&quot;
      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:
        - &quot;27017&quot;
  
      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:
        - &quot;27017&quot;
      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:
        - &quot;27017&quot;
      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:
        - &quot;27017&quot;
  
      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:
        - &quot;27017&quot;
      volumes:
        - ~/mongo_cluster/config1:/data/db
  
    mongocfg2:
      container_name: mongocfg2
      image: mongo
      command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
      expose:
        - &quot;27017&quot;
      volumes:
        - ~/mongo_cluster/config2:/data/db
  
    mongocfg3:
      container_name: mongocfg3
      image: mongo
      command: mongod --configsvr --replSet mongors1conf --dbpath /data/db --port 27017
  
      expose:
        - &quot;27017&quot;
      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:
        - &quot;27017&quot;
  
    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:
        - &quot;27017&quot;
  
  
  # TODO after running docker-compose
  # conf = rs.config();
  # conf.members[0].priority = 2;
  # rs.reconfig(conf);
  </code></pre>
  <p>And this is the script to run and create the shards etc..:</p>
  <pre><code>#!/bin/sh
  docker-compose up
  # configure our config servers replica set
  docker exec -it mongocfg1 bash -c &quot;echo 'rs.initiate({_id: \&quot;mongors1conf\&quot;,configsvr: true, members: [{ _id : 0, host : \&quot;mongocfg1\&quot; },{ _id : 1, host : \&quot;mongocfg2\&quot; }, { _id : 2, host : \&quot;mongocfg3\&quot; }]})' | mongo&quot;
  
  # building replica shard
  docker exec -it mongors1n1 bash -c &quot;echo 'rs.initiate({_id : \&quot;mongors1\&quot;, members: [{ _id : 0, host : \&quot;mongors1n1\&quot; },{ _id : 1, host : \&quot;mongors1n2\&quot; },{ _id : 2, host : \&quot;mongors1n3\&quot; }]})' | mongo&quot;
  docker exec -it mongors2n1 bash -c &quot;echo 'rs.initiate({_id : \&quot;mongors2\&quot;, members: [{ _id : 0, host : \&quot;mongors2n1\&quot; },{ _id : 1, host : \&quot;mongors2n2\&quot; },{ _id : 2, host : \&quot;mongors2n3\&quot; }]})' | mongo&quot;
  
  
  # we add shard to the routers
  docker exec -it mongos1 bash -c &quot;echo 'sh.addShard(\&quot;mongors1/mongors1n1\&quot;)' | mongo &quot;
  docker exec -it mongos1 bash -c &quot;echo 'sh.addShard(\&quot;mongors2/mongors2n1\&quot;)' | mongo &quot;
  </code></pre>
  <p>If I try to run directly the script I get the errors:</p>
  <blockquote>
  <p>mongos1     | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-07-25T09:03:56.101+00:00&quot;},&quot;s&quot;:&quot;I&quot;,  &quot;c&quot;:&quot;-&quot;,        &quot;id&quot;:4333222, &quot;ctx&quot;:&quot;ReplicaSetMonitor-TaskExecutor&quot;,&quot;msg&quot;:&quot;RSM received error response&quot;,&quot;attr&quot;:{&quot;host&quot;:&quot;mongocfg3:27017&quot;,&quot;error&quot;:&quot;HostUnreachable: Error connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by :: Connection refused&quot;,&quot;replicaSet&quot;:&quot;mongors1conf&quot;,&quot;response&quot;:&quot;{}&quot;}}</p>
  </blockquote>
  <blockquote>
  <p>mongos1     | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-07-25T09:03:56.101+00:00&quot;},&quot;s&quot;:&quot;I&quot;,
  &quot;c&quot;:&quot;NETWORK&quot;,  &quot;id&quot;:4712102,
  &quot;ctx&quot;:&quot;ReplicaSetMonitor-TaskExecutor&quot;,&quot;msg&quot;:&quot;Host failed in replica
  set&quot;,&quot;attr&quot;:{&quot;replicaSet&quot;:&quot;mongors1conf&quot;,&quot;host&quot;:&quot;mongocfg3:27017&quot;,&quot;error&quot;:{&quot;code&quot;:6,&quot;codeName&quot;:&quot;HostUnreachable&quot;,&quot;errmsg&quot;:&quot;Error
  connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by ::
  Connection
  refused&quot;},&quot;action&quot;:{&quot;dropConnections&quot;:true,&quot;requestImmediateCheck&quot;:false,&quot;outcome&quot;:{&quot;host&quot;:&quot;mongocfg3:27017&quot;,&quot;success&quot;:false,&quot;errorMessage&quot;:&quot;HostUnreachable:
  Error connecting to mongocfg3:27017 (172.18.0.2:27017) :: caused by ::
  Connection refused&quot;}}}}</p>
  </blockquote>
  <p>And other errors like:</p>
  <blockquote>
  <p>mongos1     | {&quot;t&quot;:{&quot;$date&quot;:&quot;2021-07-25T09:05:39.743+00:00&quot;},&quot;s&quot;:&quot;I&quot;,
  &quot;c&quot;:&quot;-&quot;,        &quot;id&quot;:4939300,
  &quot;ctx&quot;:&quot;monitoring-keys-for-HMAC&quot;,&quot;msg&quot;:&quot;Failed to refresh key
  cache&quot;,&quot;attr&quot;:{&quot;error&quot;:&quot;FailedToSatisfyReadPreference: Could not find
  host matching read preference { mode: &quot;nearest&quot; } for set
  mongors1conf&quot;,&quot;nextWakeupMillis&quot;:1800}}</p>
  </blockquote>
  <p>Shouldn't docker configure all the files without the user has to? Or do I need to create something manually like the database etc.?</p>
  <p>EDIT: Here there are the first errors that show up when I run the script: <a href="https://pastebin.com/4d8YkCBR" rel="nofollow noreferrer">log</a></p>