title: How to write bash script for init-mongo.sh:ro to add admin user and run an initialisation script
body: <p>My docker compose of mongo fails to authenticate when I add a docker-entrypoint-initdb.d/init-mongo.sh:ro file</p>
  <p><strong>docker-compose</strong></p>
  <pre><code>version: &quot;3.1&quot;
  services:
    mongo1:
      container_name: mongo1
      image: mongo
      restart: always
      environment:
        MONGO_INITDB_ROOT_USERNAME: ${MONGO_USER}
        MONGO_INITDB_ROOT_PASSWORD: ${MONGO_PASS}
        MONGO_INITDB_DATABASE: ${MONGO_DB_NAME}
      ports:
        - 27017:27017
        - 28017:28017
      env_file:
        - .env
      volumes:
        - volume-mongo:/data/db
        - ./mongo/init-mongo-js.sh:/docker-entrypoint-initdb.d/init-mongo.sh:ro
      command: ['--auth', '--wiredTigerCacheSizeGB=1']
      networks:
        - mongo-network
    # mongo-express:
    #   container_name: mongo-express
    #   image: mongo-express
    #   restart: always
    #   ports:
    #     - '8081:8081'
    #   env_file:
    #     - .env
    #   environment:
    #     - ME_CONFIG_OPTIONS_EDITORTHEME=ambiance
    #     - ME_CONFIG_BASICAUTH_USERNAME=${MONGO_USER}
    #     - ME_CONFIG_BASICAUTH_PASSWORD=${MONGO_PASS}
    #     - ME_CONFIG_MONGODB_ENABLE_ADMIN=true
    #     - ME_CONFIG_MONGODB_AUTH_DATABASE=admin
    #     - ME_CONFIG_MONGODB_SERVER=mongo1
    #     - ME_CONFIG_MONGODB_ADMINUSERNAME=${MONGO_USER}
    #     - ME_CONFIG_MONGODB_ADMINPASSWORD=${MONGO_PASS}
    #   depends_on:
    #     - mongo1
    #   networks:
    #     - mongo-network
  volumes:
    volume-mongo:
      driver: local
  
  networks:
    mongo-network:
      driver: bridge
  </code></pre>
  <p><strong>.env</strong></p>
  <pre><code>MONGO_DB_NAME=DBX
  MONGO_USER=MYNEWUSERNAME
  MONGO_PASS=somesecret
  # Mongo host
  </code></pre>
  <p>Attempting to login fails</p>
  <pre><code>docker exec -it mongo1 mongo admin -u MYNEWUSERNAME -p somesecret
  </code></pre>
  <p>When I remove the reference to the init-mongo.sh:ro- file then it works but then I don't get to run my initialisation script.</p>
  <p>I have been referred to this article:
  <a href="https://github.com/docker-library/mongo/issues/174" rel="nofollow noreferrer">https://github.com/docker-library/mongo/issues/174</a></p>
  <p>I'm assuming the reason it fails is because my bash file - init-mongo.sh:ro is overwriting this file:</p>
  <pre><code>
  if [ &quot;$MONGO_INITDB_ROOT_USERNAME&quot; ] &amp;&amp; [ &quot;$MONGO_INITDB_ROOT_PASSWORD&quot; ]; then 
    rootAuthDatabase='admin' 
  
    &quot;${mongo[@]}&quot; &quot;$rootAuthDatabase&quot; &lt;&lt;-EOJS 
      db.createUser({ 
        user: $(_js_escape &quot;$MONGO_INITDB_ROOT_USERNAME&quot;), 
        pwd: $(_js_escape &quot;$MONGO_INITDB_ROOT_PASSWORD&quot;), 
        roles: [ { role: 'root', db: $(_js_escape &quot;$rootAuthDatabase&quot;) } ] 
      }) 
    EOJS 
  fi 
  </code></pre>
  <p>If that theory is correct then including it into my script with my additional changes seems like a good solution. The only problem is in my vscode, with shell-format extension, the colour coding of this code combination doesn't look right so I suspect its wrong.</p>
  <p><strong>init-mongo.sh:ro</strong>
  Attempting to combine the following:</p>
  <pre><code>#!/bin/bash
  set -e
  # set -e causes the whole script to exit when a command fails, so the script can't silently fail and startup mongo.
  
  if [ &quot;$MONGO_INITDB_ROOT_USERNAME&quot; ] &amp;&amp; [ &quot;$MONGO_INITDB_ROOT_PASSWORD&quot; ]; then 
    rootAuthDatabase='admin' 
  
    &quot;${mongo[@]}&quot; &quot;$rootAuthDatabase&quot; &lt;&lt;-EOJS 
      db.createUser({ 
        user: $(_js_escape &quot;$MONGO_INITDB_ROOT_USERNAME&quot;), 
        pwd: $(_js_escape &quot;$MONGO_INITDB_ROOT_PASSWORD&quot;), 
        roles: [ { role: 'root', db: $(_js_escape &quot;$rootAuthDatabase&quot;) } ] 
      }) 
    EOJS 
  fi 
  
  mongo &lt;&lt;EOF
  use ${MONGO_DB_NAME}
  db.createCollection(&quot;users&quot;)
  db.users.insert({&quot;name&quot;: &quot;mike&quot;})
  EOF
  </code></pre>
  <p>Any help in fixing this bash script appreciated.</p>
  <p>thanks</p>