Mongo Docker没有创建用户

时间:2016-11-03 06:42:07

标签: mongodb docker dockerfile

基本上我想在创建图像时创建用户。这样我就可以在auth模式下直接启动容器了。整个过程必须自动化。因此,我遵循了以下流程。

我正在使用mongo docker office images 3.2。我的泊坞文件

FROM mongo:3.2
MAINTAINER <name> <mail.com>
LABEL description="Mongo installation."
ADD Changeauthversion.js /home/script/
ADD createadminuser.js /home/script/
ADD createsavpuser.js /home/script/
RUN mongod --fork --logpath /var/log/mongodb.log \
&& sleep 5 \
&&mongo <Databasename> /home/script/Changeauthversion.js \
&& mongo <Databasename> /home/script/createadminuser.js \
&& mongo <Databasename> /home/script/createuser.js \
&& rm -r /tmp/mongodb-27017.sock \
&& rm -r /etc/mongod.conf.orig
ADD mongod.conf.orig /etc/
EXPOSE 5002

Docker运行文件

docker RUN -v mongoDb:/data/db -p 27018:27017 -p 28017:28017 --name mongodb1 -d platform_mongodb:v1

Changeauthversion.js

db.system.users.remove({});
db.system.version.remove({});
db.system.version.insert({ "_id" : "authSchema", "currentVersion" : 3 });

createadminuser.js

db.createCollection("test");
db.createUser({ user: "user",
pwd: "pwd",
roles: [
{ role: "userAdmin", db: "admin" },
{ role: "userAdminAnyDatabase", db: "admin" },
]
});

createuser.js

db.createUser({ user: "user",
pwd: "pwd",
roles: [
{ role: "readWrite", db: "databasename" } ,
]
});

创建图像时记录

about to fork child process, waiting until server is ready for   connections.
forked process: 7
child process started successfully, parent exiting
MongoDB shell version: 3.2.10
connecting to: admin
MongoDB shell version: 3.2.10
connecting to: admin
Successfully added user: {
    "user" : "admin",
    "roles" : [
            {
                    "role" : "userAdmin",
                    "db" : "admin"
            },
            {
                    "role" : "userAdminAnyDatabase",
                    "db" : "admin"
            }
    ]
}
MongoDB shell version: 3.2.10
connecting to: database
Successfully added user: {
    "user" : "user",
    "roles" : [
            {
                    "role" : "readWrite",
                    "db" : "database"
            }
    ]
}
 ---> 98538e078e6e
Removing intermediate container 2ba4fbe3c493
Step 9 : ADD mongod.conf.orig /etc/
 ---> d9d72e70cf3b
Removing intermediate container 68d9c8d43ae8
Step 10 : EXPOSE 5002
---> Running in c134feaec53c
 ---> 296888ad5d23
Removing intermediate container c134feaec53c
Successfully built 296888ad5d23

当我启动容器并尝试登录时,mongo会抛出一个错误:找不到用户。

容器中的错误日志

:24:35.160+0000 I NETWORK  [initandlisten] connection accepted from    10.0.2.2:49713 #2 (2 connections now open)
:24:35.165+0000 I ACCESS   [conn2] SCRAM-SHA-1 authentication failed for admin on admin from client 10.0.2.2 ; UserNotFound: Could not find user admin@admin

我已经在mongo.conf文件中启用了授权。我在构建过程中将其复制到图像中。

security:
authorization: enabled
你能告诉我这是什么问题吗?为什么用户没有反映在容器中。还有另一种方法。

2 个答案:

答案 0 :(得分:3)

您可以通过使用您自己的mongo:3.2 image CMD指令来覆盖此问题,并绑定脚本以在图像启动后运行。您还可以创建一个shell脚本来运行容器,然后在容器启动后创建docker exec脚本。无论哪种方式 - 脚本应该在运行时执行,而不是构建时间。

答案 1 :(得分:0)

How to create a Mongo Docker Image with default collections and data?

如果您浏览此链接。它解释了与卷有关的docker行为。它还解释了为什么数据没有持久化。我已经尝试过上面链接中显示的方法。