简体   繁体   中英

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. 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 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" } ,
]
});

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.

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. 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. You can also create a shell script to run the container and then docker exec the scripts after the container is started. 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?

If you go through this link. It explains docker behavior with respect to volumes. It also explains why data was not persisted. I have tried it the method shown in the above link works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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