Mongodb Docker - Creating initial users and setting up initial structures

Viewed 1670

I am trying to build a mongodb container with a user and basic database structure already setup (from Dockerfile).

Here is my Dockerfile:

FROM mongo:latest

RUN mongo localhost:27017 "db.help()"

(minified it for testing purposes).

I keep getting the error:

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

...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.

However, if I remove that RUN command and manually do:

docker -exec -it mongodb_container bash
mongo
> db.help()

...it works.

I have tried setting ownership to mongodb:mongodb and tried moving log and db path to ~/db dir without success.

PS: I am using docker-compose. Would like to do this with docker-compose so developers do not have to manage things themselves.

Here is docker-compose segment:

mongodb:
  build: ./mongodb
  ports:
    - "27017:27017"

EDIT:

If I change it to 0.0.0.0:27017, then I get this error:

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

EDIT2:

When I try with following Dockerfile:

FROM mongo:latest

RUN mkdir -p /data/db2 \
    && echo "dbpath = /data/db2" > /etc/mongodb.conf \
    && chown -R mongodb:mongodb /data/db2

COPY . /data/db2

RUN mongod --fork --logpath /var/log/mongodb.log --dbpath /data/db2

MongoDB starts initially, but then exits with output:

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
1 Answers
Related