4

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
4
  • What port is Mongo listening in inside the container? Seems like it doesn't bind to port 27017. Commented Jun 1, 2017 at 14:59
  • Afaik the parent Dockerfile does make it listen on 27017/27018/27019. Or am I mistaken? Commented Jun 1, 2017 at 15:07
  • Yup sorry, didn't parse the command line with the localhost:27017. Might try binding to 0.0.0.0:27017 Commented Jun 1, 2017 at 15:10
  • Tried, says Couldn't connect to server. Also, please check edits above in post. Commented Jun 1, 2017 at 16:05

1 Answer 1

2

You can use a slight different approach. You can feed mongo with data whenever it starts at first time. You can accomplish that using an auxiliar container mongo-seed.

See this approach:

version: "2"

mongodb:
  image: mongo
  ports:
    - "27017:27017"
mongo-seed:
  image: mongo
  command: mongoimport --host mongodb --db example-db --collection MyDummyCollection --type json --file /init.json --jsonArray
  volumes:
    - ./init.json:/init.json

init.json

[
  {
    "name": "Joe Smith",
    "email": "[email protected]",
    "age": 40,
    "admin": false
  },
  {
    "name": "Jen Ford",
    "email": "[email protected]",
    "age": 45,
    "admin": true
  }
]

Run as:

docker-compose up
docker-compose exec mongodb mongo
> use example-db
> db.MyDummyCollection.find()
{ "_id" : ObjectId("592f5496fcdcafc0a8e0e0c8"), "name" : "Joe Smith", "email" : "[email protected]", "age" : 40, "admin" : false }
{ "_id" : ObjectId("592f5496fcdcafc0a8e0e0c9"), "name" : "Jen Ford", "email" : "[email protected]", "age" : 45, "admin" : true }

I took the code from here, but I've improved it without the need of a Dockerfile for the seed.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, will give it a shot later

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.