公式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
--authdocker entrypoint.shスクリプトは環境変数が存在する場合にこれを追加するため、コマンドラインで使用する必要はありません/使用できません。
データベースの初期化
このイメージは、データベースの初期化時に1回実行される/docker-entrypoint-initdb.d/カスタムスクリプト.jsまたは.shセットアップスクリプトを展開するためのパスも提供します。.jsスクリプトはtest、デフォルトで、またはMONGO_INITDB_DATABASE環境で定義されている場合に実行されます。
COPY mysetup.sh /docker-entrypoint-initdb.d/
または
COPY mysetup.js /docker-entrypoint-initdb.d/
データを使用したコレクションのセットアップ、ロギング、およびエラーで終了する方法(結果チェック用)を示す単純な初期化mongoシェルjavascriptファイルcontainer。
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' }),
db.other.
]
printjson(res)
if (error) {
print('Error, exiting')
quit(1)
}