title: How to apply authentication to mongo cluster composed of container?
body: <pre><code>CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS                      NAMES
  8ed380b215fe        mongo               &quot;docker-entrypoint.s…&quot;   2 hours ago         Up 2 hours          0.0.0.0:50001-&gt;27017/tcp   shard1svr1
  854d9f579a87        mongo               &quot;docker-entrypoint.s…&quot;   2 hours ago         Up 2 hours          0.0.0.0:40001-&gt;27017/tcp   cfgsvr1
  7a7cad24246c        mongo               &quot;docker-entrypoint.s…&quot;   3 hours ago         Up About an hour    0.0.0.0:37017-&gt;27017/tcp   mongos
  </code></pre>
  <p>I have configured a mongo cluster as above. This is the docker-compose file used to create each.</p>
  <pre><code>mongos docker-compose.yaml
  
  version: '3'
  
  services:
  
    mongos:
      container_name: mongos
      image: mongo
      command: mongos --configdb cfgrs/192.168.0.3:40001 --bind_ip 0.0.0.0 --port 27017
      ports:
        - 37017:27017
  </code></pre>
  <pre><code>config server docker-compose.yaml
  version: '3'
  
  services:
  
    cfgsvr1:
      container_name: cfgsvr1
      image: mongo
      command: mongod --configsvr --replSet cfgrs --port 27017 --dbpath /data/db
      ports:
        - 40001:27017
      volumes:
        - cfgsvr1:/data/db
  
  volumes:
    cfgsvr1: {}
  </code></pre>
  <pre><code>shard server docker-compose.yaml
  
  version: '3'
  
  services:
  
    shard1svr1:
      container_name: shard1svr1
      image: mongo
      command: mongod --shardsvr --replSet shard1rs --port 27017 --dbpath /data/db
      ports:
        - 50001:27017
      volumes:
        - shard1svr1:/data/db
  
  volumes:
    shard1svr1: {}
  </code></pre>
  <p>After creation, I created an admin account, created a specific db, and created a user in a specific db.</p>
  <pre><code>mongos&gt; show dbs
  EV           0.761GB
  admin        0.000GB
  config       0.001GB
  elex         0.000GB
  sampleData   0.507GB
  sampleData2  0.003GB
  mongos&gt; show users
  mongos&gt; use admin
  switched to db admin
  mongos&gt; show users
  {
          &quot;_id&quot; : &quot;admin.admin&quot;,
          &quot;userId&quot; : UUID(&quot;1b1e25ad-4c54-4110-bafc-cb9643963d11&quot;),
          &quot;user&quot; : &quot;admin&quot;,
          &quot;db&quot; : &quot;admin&quot;,
          &quot;roles&quot; : [
                  {
                          &quot;role&quot; : &quot;userAdminAnyDatabase&quot;,
                          &quot;db&quot; : &quot;admin&quot;
                  }
          ],
          &quot;mechanisms&quot; : [
                  &quot;SCRAM-SHA-1&quot;,
                  &quot;SCRAM-SHA-256&quot;
          ]
  }
  
  mongos&gt; use elex
  switched to db elex
  mongos&gt; show users
  {
          &quot;_id&quot; : &quot;elex.elex&quot;,
          &quot;userId&quot; : UUID(&quot;8bdba5c5-41fc-4342-a68e-f930134615e9&quot;),
          &quot;user&quot; : &quot;elex&quot;,
          &quot;db&quot; : &quot;elex&quot;,
          &quot;roles&quot; : [
                  {
                          &quot;role&quot; : &quot;readWrite&quot;,
                          &quot;db&quot; : &quot;elex&quot;
                  }
          ],
          &quot;mechanisms&quot; : [
                  &quot;SCRAM-SHA-1&quot;,
                  &quot;SCRAM-SHA-256&quot;
          ]
  }
  
  </code></pre>
  <p>However, you can access the db by simply url and port number without user id and pw from outside.</p>
  <pre><code>mongo &quot;IP:PORT&quot; -u admin -p &quot;pass&quot; --authenticationDatabase admin
  </code></pre>
  <p>Setting authenticationDatabase still doesn't take effect. How can I apply authentication?</p>