title: Docker MongoDB : UserNotFound
body: <p>I am building a non-interactively configurable MongoDB image for Docker, with the current goal to be able to load auth users from a config file programmatically. My current problem is that albeit I can successfully add users to the database, they seem not to persist through the build process and as such my node container cannot successfully authenticate against the database.</p>
  
  <p>Here's the main dockerfile for my mongodb instance:</p>
  
  <pre><code>FROM mongo:latest
  
  #Installing Node
  RUN apt-get update &amp;&amp; \
      apt-get install -y curl &amp;&amp; \
      curl -sL https://deb.nodesource.com/setup_6.x | bash - &amp;&amp; \
      apt-get install -y nodejs
  
  #Setting Up Mongo
  WORKDIR /var/www/smq
  COPY ./docker/mongo-setup.js mongo-setup.js
  COPY ./.config/mongo /var/www/.config/mongo
  RUN if [ -e /var/www/.config/mongo/mongod.conf ] ; then cp /var/www/.config/mongo/mongod.conf /etc/mongod.conf ; fi
  RUN mongod --port 27017 &amp; node mongo-setup.js &amp;&amp; mongod --shutdown
  
  CMD ["mongod", "--auth", "--port", "27017"]
  </code></pre>
  
  <p>My setup also relies on docker-compose:</p>
  
  <pre><code>  mongodb:
      build:
        context: ../
        dockerfile: ./docker/mongodb-dockerfile
      container_name: mongo_container
      volumes:
       - /data/db
  </code></pre>
  
  <p>At step <code>RUN mongod --port 27017 &amp; node mongo-setup.js &amp;&amp; mongod --shutdown</code>, I get the print showing successful creation of users via the mongo shell. However when the two docker containers (mongo &amp; node) are connected, I get the following error from the mongod : </p>
  
  <blockquote>
    <p>2017-01-02T20:43:14.895+0000 I ACCESS   [conn1] SCRAM-SHA-1 authentication failed for mydbuser on mydb from client 172.20.0.3:49762 ; UserNotFound: Could not find user mydbuser@mydb</p>
  </blockquote>
  
  <p>How can I resolve this issue?</p>
  
  <p>Ultimately, this is an effort to open-source my setup, hence you can reproduce the step by pulling <a href="https://github.com/p-hebert/docker/tree/node-mongo/node-mongo" rel="noreferrer">this repository</a> from github (currently the said setup is under <code>/node-mongo</code> on the branch <code>node-mongo</code>). Cheers!</p>