如何在启动时为 MongoDB 容器创建数据库? [英] How to create a DB for MongoDB container on start up?

查看:42
本文介绍了如何在启动时为 MongoDB 容器创建数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Docker,我有一个包含 PHP、MySQL、Apache 和 Redis 的堆栈.我现在需要添加 MongoDB,所以我正在检查 Dockerfile最新版本以及docker-entrypoint.sha> 来自 MongoDB Dockerhub 的文件,但我找不到设置docker-compose.yml 文件中容器的默认数据库、管理员用户/密码和可能的身份验证方法.

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.

在 MySQL 中,您可以设置一些 ENV 变量,例如:

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}

这会将数据库和用户/密码设置为 root 密码.

And this will setup the DB and the user/password as the root password.

有什么办法可以用 MongoDB 实现同样的效果吗?任何人都有一些经验或解决方法?

Is there any way to achieve the same with MongoDB? Anyone has some experience or workaround?

推荐答案

官方mongo 图像合并了一个 PR 以包含该功能 在启动时创建数据库和管理员用户.

The official mongo image has merged a PR to include the functionality to create databases and admin users at startup.

/data/db 目录中没有填充任何内容时,数据库初始化将运行 /docker-entrypoint-initdb.d/ 中的任何脚本.

The database initialisation will run any scripts in /docker-entrypoint-initdb.d/ when there is nothing populated in the /data/db directory.

mongo 容器镜像提供了 /docker-entrypoint-initdb.d/ 路径来部署自定义 .js.sh 设置脚本,将在数据库初始化时运行一次..js 脚本将默认针对 testMONGO_INITDB_DATABASE(如果在环境中定义)运行.

The mongo container image provides the /docker-entrypoint-initdb.d/ 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/

COPY mysetup.js /docker-entrypoint-initdb.d/

一个简单的初始化mongo shell javascript 文件,演示了设置container 包含数据、日志记录以及如何退出错误(用于结果检查)的集合.

A simple initialisation mongo shell javascript file that demonstrates setting up the container collection with data, logging and how to exit with an error (for result checking).

let error = true

let res = [
  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)
}

管理员用户设置

控制root"的环境变量用户设置是

Admin User Setup

The environment variables to control "root" user setup are

  • MONGO_INITDB_ROOT_USERNAME
  • MONGO_INITDB_ROOT_PASSWORD

示例

docker run -d 
  -e MONGO_INITDB_ROOT_USERNAME=admin 
  -e MONGO_INITDB_ROOT_PASSWORD=password 
  mongod

Dockerfile

FROM docker.io/mongo
ENV MONGO_INITDB_ROOT_USERNAME admin
ENV MONGO_INITDB_ROOT_PASSWORD password

您不需要在命令行上使用 --auth,因为 docker entrypoint.sh 脚本会在检测到环境变量存在时添加它.

You don't need to use --auth on the command line as the docker entrypoint.sh script adds this in when it detects the environment variables exist.

这篇关于如何在启动时为 MongoDB 容器创建数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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