简体   繁体   English

Mongo Docker未创建用户

[英]Mongo Docker not creating user

Basically i want to create users while creating the image. 基本上我想在创建图像时创建用户。 So that i can directly start my container in auth mode. 这样我就可以直接在身份验证模式下启动我的容器了。 The whole process must be automated. 整个过程必须自动化。 Hence i have followed the below process. 因此,我遵循以下过程。

I am using mongo docker office images 3.2. 我正在使用mongo docker office images 3.2。 My docker file 我的码头工人文件

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 run file Docker运行文件

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

Changeauthversion.js Changeauthversion.js

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

createadminuser.js createadminuser.js

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

createuser.js createuser.js

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

Log while creating the image 创建图像时记录

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

When i start the container up and try to login, mongo throws an error:user not found. 当我启动容器并尝试登录时,mongo抛出错误:找不到用户。

Error logs from container 来自容器的错误日志

: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

I have already enabled authorization in mongo.conf file. 我已经在mongo.conf文件中启用了授权。 Which i have copied to the image during its build. 我在构建期间已将其复制到图像。

security:
authorization: enabled

Can you let me know what the issue is. 你能告诉我问题是什么吗? Why are the users not reflecting in the container. 用户为何不反映在容器中。 Is there another approach. 还有另一种方法。

You can overcome the problem by overriding the mongo:3.2 image CMD directive with your own, and rigging the scripts to run after the image is started. 您可以通过使用自己的mongo:3.2 image CMD指令覆盖并绑定脚本以在映像启动后运行来克服此问题。 You can also create a shell script to run the container and then docker exec the scripts after the container is started. 您还可以创建一个Shell脚本来运行该容器,然后在容器启动后docker exec这些脚本。 Either way - the scripts should be executed at run time, not build time. 无论哪种方式-脚本都应在运行时而非构建时执行。

How to create a Mongo Docker Image with default collections and data? 如何使用默认集合和数据创建Mongo Docker映像?

If you go through this link. 如果您通过此链接。 It explains docker behavior with respect to volumes. 它解释了Docker关于卷的行为。 It also explains why data was not persisted. 它还说明了为什么数据没有持久化。 I have tried it the method shown in the above link works. 我已经尝试了上面链接中显示的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM