如何使用docker-compose在mongodb中创建用户 [英] How to create user in mongodb with docker-compose
问题描述
晚安,
我正在尝试创建一些脚本,将使用mongodb创建一个docker并自动创建一个用户。
我通常可以用docker-compose管理我的码头图像,但是这次我不知道该怎么做。
这里是我要做的:
1)清理/破坏容器(docker-compose down)
2)使用mongodb创建一个docker容器并启动它(不带--auth参数)
3)执行包含db.createUser()的java脚本
4)停止容器
5)使用--auth参数重新启动相同的容器,以允许用户创建的用户登录javascript
我无法找到如何使用docker-compose正常进行,因为在启动时,我必须给它命令--auth。如果我这样做,我不能执行我的javascript添加我的用户。 MongoDB允许用户创建而不登录,如果没有用户,并且没有提供--auth参数。
我想自动做,我不想手动执行某些命令。目标是在每次集成测试之前都要执行一个脚本,从一个干净的数据库开始。
这是我的项目:
集成测试/ src / test / resources / scripts / docker-compose.yml
mongodb:
container_name:mongo
image:mongo
ports:
- 27017:27017
volumes:
- 。:/ setup
command :--auth
集成测试/ src / test / resources / scripts / docker-init.sh
docker-compose down
docker-compose up -d
sleep 1
docker exec mongo bash -cmongo myDatabase /setup/mongodb-setup.js
integration-test / src /test/resources/scripts/mongodb-setup.js
db.createUser(
{
用户:myUser,
pwd:myPassword,
roles:[
{role:readWrite,db:myDatabase}
]
})
查找重新启动一个新参数(在这种情况下为aa)的容器的方法将有所帮助,但是我找不到如何做(Docker start不参数)。
$ b任何想法我应该怎么做我想要的?
如果没有,我仍然可以从我的数据库中删除一些Java代码或其他东西,但我想要使用脚本创建一个完整的mongodb docker设置。
谢谢!
我建议您使用环境变量来设置mongo用户,数据库和密码。 tutum
(由Docker拥有)发布了一个非常好的图像
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 =mypasstutum / mongodb
您可以将这些变量转换为docker-compose环境变量。你不必硬编码。
环境:
MONGODB_USER:$ {db_user_env}
MONGODB_DATABASE:$ {dbname_env}
MONGODB_PASS:$ {db_pass}
此配置将从会话的环境变量。
Good evening,
I'm trying to create some kind of script that will create a docker with mongodb and automatically create a user.
I can usually manage my docker images with docker-compose but this time, I don't know how to do it.
Basically, here is what I have to do :
1) clean/destroy container (docker-compose down)
2) create a docker container with mongodb and start it (without --auth parameter)
3) execute a java script containing db.createUser()
4) stop the container
5) restart the same container with --auth parameter to allow login with the user created in the 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.
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
mongodb:
container_name: mongo
image: mongo
ports:
- "27017:27017"
volumes:
- .:/setup
command: --auth
integration-test/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
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).
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.
Thank you!
I suggest that you use environment variables to set mongo user, database and password. tutum
(owned by Docker) published a very good image
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.
environment:
MONGODB_USER: "${db_user_env}"
MONGODB_DATABASE: "${dbname_env}"
MONGODB_PASS: "${db_pass}"
This configuration will read from your session's environment variables.
这篇关于如何使用docker-compose在mongodb中创建用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!