Mongodb Docker - 创建初始用户并设置初始结构

时间:2017-06-01 14:56:05

标签: mongodb docker docker-compose dockerfile

我正在尝试构建一个mongodb容器,其中已经设置了用户和基本数据库结构(来自Dockerfile)。

这是我的Dockerfile:

FROM mongo:latest

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

(将其缩小以用于测试目的)。

我一直收到错误:

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

...并且容器无法构建。我甚至不确定如何检查日志,因为我从Dockerfile构建,一旦容器无法构建,就无法进入检查日志。

但是,如果我删除该RUN命令并手动执行:

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

......它有效。

我已尝试将所有权设置为mongodb:mongodb并尝试将日志和数据库路径移至〜/ db目录但未成功。

PS:我正在使用docker-compose。想用docker-compose做这件事,这样开发人员就不必自己管理。

这是docker-compose段:

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

修改

如果我将其更改为0.0.0.0:27017,则会出现此错误:

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:

当我尝试使用以下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最初启动,但随后退出并输出:

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 个答案:

答案 0 :(得分:2)

您可以使用略微不同的方法。只要第一次启动,您就可以为mongo提供数据。您可以使用辅助容器mongo-seed完成此操作。

见这种方法:

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": "jsmith@gmail.com",
    "age": 40,
    "admin": false
  },
  {
    "name": "Jen Ford",
    "email": "jford@gmail.com",
    "age": 45,
    "admin": true
  }
]

运行方式:

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

我从here获取了代码,但是我已经改进了它,而不需要种子的Dockerfile。