title: MongoDB and Docker via docker-compose auth error and not initalizing db
body: <p>I am new to Docker and I have read the <a href="https://docs.docker.com/samples/library/mongo/" rel="nofollow noreferrer">Mongo Docker Docs</a>.
  I need some help setting up a simple application.</p>
  
  <p>I want a local mongodb that dumps / uses a database named "my_test_db"
  and writes to <code>&lt;project-dir&gt;/mongo/my_test_db</code></p>
  
  <p>Here is my simple project:</p>
  
  <pre class="lang-sh prettyprint-override"><code># project structure
  &lt;project-dir&gt;/
  -- .env
  -- docker-compose.yml
  -- mongo/
  -- + -- my_test_db/
  
  </code></pre>
  
  <p>The <code>.env</code> file:</p>
  
  <pre class="lang-sh prettyprint-override"><code># &lt;project&gt;/.env
  MONGO_INITDB_ROOT_USERNAME=test_user
  MONGO_INITDB_ROOT_PASSWORD=test_pwrd
  MONGO_INITDB_DATABASE=my_test_db
  MONGO_DATA_DIR=/mongo/my_test_db
  MONGO_LOG_FILE=/mongo/mongodb.log
  
  </code></pre>
  
  <p>my attempt at <code>docker compose</code>:</p>
  
  <pre><code>#docker-compose.yml
  
  version: '3'
  services:
    mongodb:
      image: mongo:4.0
  
      # load env variables
      env_file:
        - .env 
  
      # map data_dir to host &lt;project&gt;/&lt;data_dir&gt;
      volumes:
        - ".${MONGO_DATA_DIR}:${MONGO_DATA_DIR}"
  
      expose:
        - 27017
  
      # set env vars with loaded vars (appears to be necessary for the user to be created, despite loading them with env_file)
      environment:
        MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME}
        MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_DATABASE}
        MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE}
  
      # map ports
      ports:
          - 27017:27017
      command: "mongod --smallfiles"
      container_name: mongodb-test
  # --logpath=.${MONGO_LOG_FILE} # when added to command fails
  
  </code></pre>
  
  <p>then in two shells I have</p>
  
  <pre class="lang-sh prettyprint-override"><code>#shell-1
  &lt;project-dir&gt;$ docker-compose up
  
  # I see my user created
  # I see "MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=e19ea190b601" so my custom db isnt made on init...
  </code></pre>
  
  <pre class="lang-sh prettyprint-override"><code>#shell-2
  # &lt;project-dir&gt;$ mongo 27017 --usernamne test_user --password test_pwrd # does fails to connect
  &lt;project-dir&gt;$ mongo 27017 # connects
  
  
  
  &gt; db.getName()
  # 27017 # so naming it didnt work
  
  &gt; show users
  # Error: command usersInfo requires authentication
  
  &gt; use my_test_db
  &gt; db.myColl.insert({'name': 'test'})
  # WriteCommandError({..., "codeName": "Unauthorized"})
  
  
  </code></pre>
  
  <p>I am currently testing on macOS (as docs state that this might be relevant)</p>
  
  <p>What am I doing wrong? I understand the concept of docker but personally don't use it as it is a pain to get used too... but I'm trying to learn.</p>