大网数据广告活动
服务器运维问答

Create a new user for MongoDB inside Docker

· 52CE 我爱测

I'm using the default MongoDB Docker image and I'm trying to create a new user for the database. I'm currently trying to do it this way: FROM docker.io/mongo:3.2 MAINTAINER <alexandernst> [email omitted] ADD create_ddbb.js /tmp/ …

问题原文

原帖链接 · alexandernst · CC BY-SA 3.0

I'm using the default MongoDB Docker image and I'm trying to create a new user for the database.

I'm currently trying to do it this way:

FROM docker.io/mongo:3.2

MAINTAINER <alexandernst> [email omitted]

ADD create_ddbb.js /tmp/

RUN mongod --fork --logpath /var/log/mongodb.log \
    && sleep 5 && mongo foobar /tmp/create_ddbb.js 

And the create_ddbb.js:

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

And when I build the Dockerfile, I see:

Step 4 : RUN mongod --fork --logpath /var/log/mongodb.log       && sleep 5 && mongo foobar /tmp/create_ddbb.js
 ---> Running in 58ba44d02508
about to fork child process, waiting until server is ready for connections.
forked process: 9
child process started successfully, parent exiting
MongoDB shell version: 3.2.6
connecting to: foobar
Successfully added user: {
        "user" : "***********",
        "roles" : [
                {
                        "role" : "readWrite",
                        "db" : "foobar"
                }
        ]
}
 ---> e73b6c8c8b83
Removing intermediate container 58ba44d02508
Successfully built e73b6c8c8b83

so the user is created, but then when I try to connect, I get:

mongo_1  | 2016-05-13T17:44:02.159+0000 I NETWORK  [initandlisten] connection accepted from 172.20.0.4:41294 #1 (1 connection now open)
mongo_1  | 2016-05-13T17:44:02.160+0000 I ACCESS   [conn1] SCRAM-SHA-1 authentication failed for ********* on foobar from client 172.20.0.4 ; UserNotFound: Could not find user *********@foobar
mongo_1  | 2016-05-13T17:44:02.160+0000 I NETWORK  [conn1] end connection 172.20.0.4:41294 (0 connections now open)

Why is that happening? How can I persist the created user?

已采纳解答

原帖链接 · alexandernst · CC BY-SA 3.0

My solution:

Inside your Dockerfile:

ADD create_ddbb.js /tmp/

RUN mongod -f /etc/mongod.conf --fork --logpath /var/log/mongodb.log \
    && sleep 5 \
    && mongo <YOUR DATABASE> /tmp/create_ddbb.js

Inside the create_ddbb.js:

db.createUser(
    {
      user: "your_user",
      pwd: "********************",
      roles: [
         { role: "dbOwner", db: "your_database" }
      ]
    }
,
    {
        w: "majority",
        wtimeout: 5000
    }
);
db.createCollection("test");

The createColleciton("test") at the end is extremely important. Without that, the createUser isn't applied. I don't know why exactly, sorry.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2016-05-13)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。