title: Mongodb Docker - Creating initial users and setting up initial structures
body: <p>I am trying to build a mongodb container with a user and basic database structure already setup (from Dockerfile).</p>
  
  <p>Here is my Dockerfile:</p>
  
  <pre><code>FROM mongo:latest
  
  RUN mongo localhost:27017 "db.help()"
  </code></pre>
  
  <p>(minified it for testing purposes).</p>
  
  <p>I keep getting the error:</p>
  
  <pre><code>connecting to: localhost:27017
  2017-06-01T14:49:34.353+0000 W NETWORK  [thread1] Failed to connect to 127.0.0.1:27017, in(checking socket for error after poll), reason: Connection refused
  2017-06-01T14:49:34.353+0000 E QUERY    [thread1] Error: couldn't connect to server localhost:27017, connection attempt failed :
  connect@src/mongo/shell/mongo.js:237:13
  @(connect):1:6
  exception: connect failed
  </code></pre>
  
  <p>...and the container fails to build. I am not even sure how to check the logs since I am building from Dockerfile and once the container fails to build, there's no way to go inside to check logs.</p>
  
  <p>However, if I remove that RUN command and manually do:</p>
  
  <pre><code>docker -exec -it mongodb_container bash
  mongo
  &gt; db.help()
  </code></pre>
  
  <p>...it works.</p>
  
  <p>I have tried setting ownership to mongodb:mongodb and tried moving log and db path to ~/db dir without success.</p>
  
  <p>PS: I am using docker-compose. Would like to do this with docker-compose so developers do not have to manage things themselves.</p>
  
  <p>Here is docker-compose segment:</p>
  
  <pre><code>mongodb:
    build: ./mongodb
    ports:
      - "27017:27017"
  </code></pre>
  
  <p><strong>EDIT:</strong></p>
  
  <p>If I change it to <code>0.0.0.0:27017</code>, then I get this error:</p>
  
  <pre><code>MongoDB shell version v3.4.4
  connecting to: 0.0.0.0:27017
  2017-06-01T15:52:52.806+0000 E QUERY    [thread1] Error: couldn't connect to server 0.0.0.0:27017, address resolved to 0.0.0.0 :
  connect@src/mongo/shell/mongo.js:237:13
  @(connect):1:6
  exception: connect failed
  </code></pre>
  
  <p><strong>EDIT2:</strong></p>
  
  <p>When I try with following Dockerfile:</p>
  
  <pre><code>FROM mongo:latest
  
  RUN mkdir -p /data/db2 \
      &amp;&amp; echo "dbpath = /data/db2" &gt; /etc/mongodb.conf \
      &amp;&amp; chown -R mongodb:mongodb /data/db2
  
  COPY . /data/db2
  
  RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2
  </code></pre>
  
  <p>MongoDB starts initially, but then exits with output:</p>
  
  <pre><code>mongodb_1    | 2017-06-01T15:55:52.955+0000 I CONTROL  [initandlisten] MongoDB starting : pid=1 port=27017 dbpath=/data/db 64-bit host=64a317041edd
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] db version v3.4.4
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] git version: 888390515874a9debd1b6c5d36559ca86b44babd
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] OpenSSL version: OpenSSL 1.0.1t  3 May 2016
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] allocator: tcmalloc
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] modules: none
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] build environment:
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten]     distmod: debian81
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten]     distarch: x86_64
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten]     target_arch: x86_64
  mongodb_1    | 2017-06-01T15:55:52.956+0000 I CONTROL  [initandlisten] options: {}
  mongodb_1    | 2017-06-01T15:55:52.964+0000 E NETWORK  [initandlisten] Failed to unlink socket file /tmp/mongodb-27017.sock Operation not permitted
  mongodb_1    | 2017-06-01T15:55:52.964+0000 I -        [initandlisten] Fatal Assertion 28578 at src/mongo/util/net/listen.cpp 194
  mongodb_1    | 2017-06-01T15:55:52.964+0000 I -        [initandlisten]
  mongodb_1    |
  mongodb_1    | ***aborting after fassert() failure
  mongodb_1    |
  mongodb_1    |
  stuff_mongodb_1 exited with code 14
  </code></pre>