MongoDB docker-entrypoint 忽略用于创建用户和数据库的自定义脚本。我试过很多方法。这是我的配置之一。
解决方案 1
docker-compose.yml(版本 1)
version: '3.2'
services:
erste-mongodb:
build:
context: .
dockerfile: mongodb.dockerfile
image: erste/lts:mongodb
#environment:
# MONGO_INITDB_ROOT_USERNAME: "root"
# MONGO_INITDB_ROOT_PASSWORD: "toor"
volumes:
- ./data/mongo-data:/data/db
# - ./data/mongo-init:/docker-entrypoint.d/
ports:
- "27017:27017"
networks:
- mynet
网络:mynet:
dockerfile
FROM mongo:latest
COPY ./data/mongo-init/mongo-init.sh /docker-entrypoint.d/
解决方案 2
docker-compose.yml(版本 2)
version: '3.2'
services:
erste-mongodb:
build:
context: .
dockerfile: mongodb.dockerfile
image: erste/lts:mongodb
environment:
MONGO_INITDB_ROOT_USERNAME: "root"
MONGO_INITDB_ROOT_PASSWORD: "toor"
volumes:
- ./data/mongo-data:/data/db
- ./data/mongo-init:/docker-entrypoint.d/
ports:
- "27017:27017"
networks:
- mynet
网络:mynet:
dockerfile
FROM mongo:latest
这是我的初始化脚本:
mongo-init.sh
#!/bin/bash
echo "Creating users..."
mongo --eval "db.createUser({user: 'revision', pwd: 'rev123',roles: [{role: 'dbOwner', db: 'ebmn_log'}]});"
mongo -u revision -p rev123 --eval "db = db.getSiblingDB('ebmn_log');
db.createCollection('journal')";
echo "Finishing with users"
有谁知道如何运行初始化脚本。
先感谢您。