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.