如何使用 docker-compose 在 mongodb 中创建用户
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37423659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create user in mongodb with docker-compose
提问by jOHNdOE
I'm trying to create some kind of script that will create a docker with mongodb and automatically create a user.
我正在尝试创建某种脚本,该脚本将使用 mongodb 创建一个 docker 并自动创建一个用户。
I can usually manage my docker images with docker-compose but this time, I don't know how to do it.
我通常可以使用 docker-compose 管理我的 docker 图像,但是这次,我不知道该怎么做。
Basically, here is what I have to do:
基本上,这是我必须做的:
- clean/destroy container (
docker-compose down
) - create a docker container with mongodb and start it (without
--auth parameter
) - execute a java script containing
db.createUser()
- stop the container
- restart the same container with
--auth
parameter to allow login with the user created in the javascript
- 清洁/销毁容器 (
docker-compose down
) - 使用 mongodb 创建一个 docker 容器并启动它(没有
--auth parameter
) - 执行一个包含
db.createUser()
- 停止容器
- 使用
--auth
参数重新启动同一个容器以允许使用在 javascript 中创建的用户登录
I can't find how to do that properly with docker-compose because when it starts, I have to give it the command --auth. If I do that, I cannot execute my javascript to add my user. MongoDB allows users creation without being logged in if there is no user and if --auth parameter is not provided.
我无法找到如何使用 docker-compose 正确执行此操作,因为当它启动时,我必须给它命令 --auth。如果我这样做,我将无法执行我的 javascript 来添加我的用户。如果没有用户且未提供 --auth 参数,MongoDB 允许用户在未登录的情况下创建。
I want to do that automatically, I do not want to manually do some commands. The goal is to have a script that can be executed before each integration tests to start from a clean database.
我想自动执行此操作,我不想手动执行某些命令。目标是拥有一个可以在每次集成测试之前执行的脚本,以从干净的数据库开始。
Here is my project:
这是我的项目:
integration-test/src/test/resources/scripts/docker-compose.yml
集成测试/src/test/resources/scripts/docker-compose.yml
mongodb:
container_name: mongo
image: mongo
ports:
- "27017:27017"
volumes:
- .:/setup
command: --auth
integration-test/src/test/resources/scripts/docker-init.sh
集成测试/src/test/resources/scripts/docker-init.sh
docker-compose down
docker-compose up -d
sleep 1
docker exec mongo bash -c "mongo myDatabase /setup/mongodb-setup.js"
integration-test/src/test/resources/scripts/mongodb-setup.js
集成测试/src/test/resources/scripts/mongodb-setup.js
db.createUser(
{
user: "myUser",
pwd: "myPassword",
roles: [
{ role: "readWrite", db: "myDatabase" }
]
})
Finding a way to start again a container with a new parameter (in this case --auth
) would help but I can't find how to do that (docker start does not take parameters).
找到一种使用新参数(在这种情况下--auth
)重新启动容器的方法会有所帮助,但我找不到如何做到这一点(docker start 不接受参数)。
Any idea how I should do what I would like ?
知道我应该如何做我想做的吗?
If not, I can still delete everything from my database with some Java code or something else but I would like a complete mongodb docker setup created with a script.
如果没有,我仍然可以使用一些 Java 代码或其他东西从我的数据库中删除所有内容,但我想要一个使用脚本创建的完整 mongodb docker 设置。
采纳答案by timchunght
EDIT:tutumcloud repository is deprecated and no longer maintained, see other answers
编辑:tutumcloud 存储库已弃用且不再维护,请参阅其他答案
I suggest that you use environment variables to set mongo user, database and password. tutum
(owned by Docker) published a very good image
我建议你使用环境变量来设置 mongo 用户、数据库和密码。tutum
(由Docker拥有)发布了一个非常好的镜像
https://github.com/tutumcloud/mongodb
https://github.com/tutumcloud/mongodb
docker run -d -p 27017:27017 -p 28017:28017 -e MONGODB_USER="user" -e MONGODB_DATABASE="mydatabase" -e MONGODB_PASS="mypass" tutum/mongodb
You may convert these variables into docker-compose environments variables. You don't have to hard code it.
您可以将这些变量转换为 docker-compose 环境变量。您不必对其进行硬编码。
environment:
MONGODB_USER: "${db_user_env}"
MONGODB_DATABASE: "${dbname_env}"
MONGODB_PASS: "${db_pass}"
This configuration will read from your session's environment variables.
此配置将从会话的环境变量中读取。
回答by jzqa
The official mongo image now supports following environment variables that can be used in docker-compose as below:
官方 mongo 镜像现在支持以下可以在 docker-compose 中使用的环境变量,如下所示:
environment:
- MONGO_INITDB_ROOT_USERNAME=user
- MONGO_INITDB_ROOT_PASSWORD=password
- MONGO_INITDB_DATABASE=test
more explanation at: https://stackoverflow.com/a/42917632/1069610
回答by GreenThumb
This is how I do it, my requirement was to bring up a few containers along with mongodb, the other containers expect a user to be present when they come up, this worked for me. The good part is, the mongoClientTemp exits after the command is executed so the container doesn't stick around.
我就是这样做的,我的要求是随 mongodb 一起启动一些容器,其他容器希望用户在出现时出现,这对我有用。好的部分是, mongoClientTemp 在命令执行后退出,因此容器不会停留。
version: '2'
services:
mongo:
image: mongo:latest
container_name: mongo
ports:
- "27017:27017"
volumes:
- /app/hdp/mongo/data:/data/db
mongoClientTemp:
image: mongo:latest
container_name: mongoClientTemp
links:
- mongo:mongo
command: mongo --host mongo --eval "db.getSiblingDB('dashboard').createUser({user:'db', pwd:'dbpass', roles:[{role:'readWrite',db:'dashboard'}]});"
depends_on:
- mongo
another-container:
image: another-image:v01
container_name: another-container
ports:
- "8080:8080"
volumes:
- ./logs:/app/logs
environment:
- MONGODB_HOST=mongo
- MONGODB_PORT=27017
links:
- mongo:mongo
depends_on:
- mongoClientTemp
回答by Emanuel Müller Ramos
After reading the the official mongo docker page, I've found that you can create an admin user one single time, even if the auth option is being used. This is not well documented, but it simply works (hope it is not a feature). Therefore, you can keep using the auth option all the time.
阅读官方mongo docker 页面后,我发现即使使用了 auth 选项,您也可以一次性创建一个管理员用户。这没有很好的文档记录,但它只是有效(希望它不是一个功能)。因此,您可以一直使用 auth 选项。
I created a githubrepository with scripts wrapping up the commands to be used. The most important command lines to run are:
我创建了一个github存储库,其中包含包含要使用的命令的脚本。要运行的最重要的命令行是:
docker exec db_mongodb mongo admin /setup/create-admin.js
docker exec db_mongodb mongo admin /setup/create-user.js -u admin -p admin --authenticationDatabase admin
The first line will create the admin user (and mongo will not complain even with auth option). The second line will create your "normal" user, using the admin rights from the first one.
第一行将创建管理员用户(即使使用 auth 选项,mongo 也不会抱怨)。第二行将使用第一行的管理员权限创建您的“普通”用户。
回答by Emanuel Müller Ramos
file: docker-compose.yaml
文件:docker-compose.yaml
mongo:
image: mongo:latest
volumes_from:
- data
ports:
- "27017:27017"
command: --auth
container_name: "db_mongodb"
data:
image: mongo:latest
volumes:
- /var/lib/mongo
- ./setup:/setup
command: "true"
container_name: "db_mongodb_data"
file: .buildMongo.sh
文件:.buildMongo.sh
#!/bin/sh
docker-compose down
docker-compose up -d
sleep 1
docker exec db_mongodb mongo admin /setup/create-admin.js
docker exec db_mongodb mongo myDb /setup/create-user.js -u admin -p admin --authenticationDatabase admin
The create-admin.js and create-user.js files are commands that you use using the mongo shell. So they must be easy for you to understand. The real direction is like the jzqa answer, "environment variables". So the question here is how to create a user. I think this answers that point at least, you can check the complete setup here https://github.com/Lus1t4nUm/mongo_docker_bootstrap
create-admin.js 和 create-user.js 文件是您使用 mongo shell 使用的命令。因此,它们必须易于您理解。真正的方向就像jzqa的回答,“环境变量”。所以这里的问题是如何创建用户。我认为这至少回答了这一点,您可以在此处查看完整的设置https://github.com/Lus1t4nUm/mongo_docker_bootstrap
回答by Adam Kozyra
Mongo image provides the /docker-entrypoint-initdb.d/ path to deploy custom .js or .sh setup scripts.
Mongo 镜像提供了 /docker-entrypoint-initdb.d/ 路径来部署自定义 .js 或 .sh 安装脚本。
Check this post to get more details : How to create a DB for MongoDB container on start up?
查看这篇文章以获取更多详细信息: 如何在启动时为 MongoDB 容器创建数据库?
回答by Chandan Rajah
add --noauth option to the mongo command
在 mongo 命令中添加 --noauth 选项
extract from my docker-compose.yml file
从我的 docker-compose.yml 文件中提取
mongors:
image: mongo:latest
command: mongod --noprealloc --smallfiles --replSet mongors2 --dbpath /data/db --nojournal --oplogSize 16 --noauth
environment:
TERM: xterm
volumes:
- ./data/mongors:/data/db