如何使用 docker-compose 在 mongodb 中创建用户 [英] How to create user in mongodb with docker-compose

查看:96
本文介绍了如何使用 docker-compose 在 mongodb 中创建用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建某种脚本,该脚本将使用 mongodb 创建一个 docker 并自动创建一个用户.

I'm trying to create some kind of script that will create a docker with mongodb and automatically create a user.

我通常可以使用 docker-compose 管理我的 docker 镜像,但这次,我不知道该怎么做.

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. 清理/销毁容器(docker-compose down)
  2. 用mongodb创建一个docker容器并启动它(没有--auth参数)
  3. 执行包含 db.createUser()
  4. 的 java 脚本
  5. 停止容器
  6. 使用 --auth 参数重新启动同一个容器,以允许使用在 javascript 中创建的用户登录
  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

我无法找到如何使用 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.

这是我的项目:

integration-test/src/test/resources/scripts/docker-compose.yml

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

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

integration-test/src/test/resources/scripts/mongodb-setup.js

db.createUser(
{
    user: "myUser",
    pwd: "myPassword",
    roles: [
      { role: "readWrite", db: "myDatabase" }
    ]
})

找到一种使用新参数重新启动容器的方法(在本例中为 --auth)会有所帮助,但我找不到如何做到这一点(docker start 不带参数).

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 ?

如果没有,我仍然可以使用一些 Java 代码或其他东西从我的数据库中删除所有内容,但我想要一个使用脚本创建的完整 mongodb docker 设置.

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.

推荐答案

tutumcloud 存储库已弃用且不再维护,请参阅其他答案

tutumcloud repository is deprecated and no longer maintained, see other answers

我建议你使用环境变量来设置mongo用户、数据库和密码.tutum(Docker 旗下)发布了一个很好的镜像

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

您可以将这些变量转换为 docker-compose 环境变量.您不必对其进行硬编码.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆