如何在启动时为MongoDB容器创建数据库? [英] How to create a DB for MongoDB container on start up?
问题描述
docker-compose.yml
文件设置容器的默认数据库,管理员用户/密码和可能的auth方法。 在MySQL中,您可以设置一些ENV变量,例如:
db:
image:mysql: 5.7
env_file:.env
环境:
MYSQL_ROOT_PASSWORD:$ {MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE:$ {MYSQL_DATABASE}
MYSQL_USER:$ {MYSQL_USER}
MYSQL_PASSWORD:$ {MYSQL_PASSWORD}
这将会将数据库和用户/密码设置为code> root 密码。
MongoDB有什么办法可以实现吗?任何人都有一些经验或解决方法?
官方的mongo图片有合并了PR以包含此功能。
当 / data / db
目录中没有任何填充时,初始化将运行。
管理员用户设置
控制root用户设置的环境变量为
-
MONGO_INITDB_ROOT_USERNAME
-
MONGO_INITDB_ROOT_PASSWORD
示例
docker run -d \
-e MONGO_INITDB_ROOT_USERNAME = admin \
-e MONGO_INITDB_ROOT_PASSWORD = password \
mongod
您不需要/不能在命令行上使用 - auth
作为docker entrypoint .sh脚本在环境变量存在的时候添加它。
其他自定义设置
容器还提供路径部署自定义 .js
或 .sh
将在数据库初始化时运行一次的安装脚本。 .js
脚本将根据 test
默认运行,或 MONGO_INITDB_DATABASE
如果在环境中定义
COPY mysetup.sh /docker-entrypoint-initdb.d/
或
COPY mysetup .js /docker-entrypoint-initdb.d/
一个简单的初始化javascript文件,演示了日志记录和如何退出并出现错误(用于结果检查)。
let error = true
let res = [
db.container.drop(),
db.container.drop(),
db.container.createIndex({myfield:1},{unique:true}),
db.container.createIndex({thatfield:1}),
db.container.createIndex({thatfield:1}),
db.container.insert({myfield:'hello',thatfield :'test'}),
db.container.insert({myfield:'hello2',thatfield:'testing'}),
db.container.insert({myfield:'hello3',thatfield : ting'}),
db.container.insert({myfield:'hello3',thatfield:'testing'}),
]
printjson(res)
如果(错误){
打印('错误,退出')
退出(1)
}
I am working with Docker and I have a stack with PHP, MySQL, Apache and Redis. I need to add MongoDB now so I was checking the Dockerfile for the latest version and also the docker-entrypoint.sh file from the MongoDB Dockerhub but I couldn't find a way to setup a default DB, admin user/password and possibly auth method for the container from a docker-compose.yml
file.
In MySQL you can setup some ENV variables as for example:
db:
image: mysql:5.7
env_file: .env
environment:
MYSQL_ROOT_PASSWORD: ${MYSQL_ROOT_PASSWORD}
MYSQL_DATABASE: ${MYSQL_DATABASE}
MYSQL_USER: ${MYSQL_USER}
MYSQL_PASSWORD: ${MYSQL_PASSWORD}
And this will setup the DB and the user/password as the root
password.
Is there any way to achieve the same with MongoDB? Anyone has some experience or workaround?
The official mongo image has merged a PR to include this functionality.
The initialisation will run when there is nothing populated in the /data/db
directory.
Admin User Setup
The environment variables to control "root" user setup are
MONGO_INITDB_ROOT_USERNAME
MONGO_INITDB_ROOT_PASSWORD
Example
docker run -d \
-e MONGO_INITDB_ROOT_USERNAME=admin \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongod
You don't need to/can't use --auth
on the command line as the docker entrypoint.sh script adds this in when the environment variables exist.
Other Custom Setup
The container also provides a path to deploy custom .js
or .sh
setup scripts that will be run once on database initialisation. .js
scripts will be run against test
by default or MONGO_INITDB_DATABASE
if defined in the environment.
COPY mysetup.sh /docker-entrypoint-initdb.d/
or
COPY mysetup.js /docker-entrypoint-initdb.d/
A simple initialisation javascript file that demonstrates logging and how to exit with an error (for result checking).
let error = true
let res = [
db.container.drop(),
db.container.drop(),
db.container.createIndex({ myfield: 1 }, { unique: true }),
db.container.createIndex({ thatfield: 1 }),
db.container.createIndex({ thatfield: 1 }),
db.container.insert({ myfield: 'hello', thatfield: 'testing' }),
db.container.insert({ myfield: 'hello2', thatfield: 'testing' }),
db.container.insert({ myfield: 'hello3', thatfield: 'testing' }),
db.container.insert({ myfield: 'hello3', thatfield: 'testing' }),
]
printjson(res)
if (error) {
print('Error, exiting')
quit(1)
}
这篇关于如何在启动时为MongoDB容器创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!