简体   繁体   English

MongoDB docker-entrypoint问题

[英]MongoDB docker-entrypoint issue

MongoDB docker-entrypoint ignoring custom script for creating user and database. MongoDB docker-entrypoint忽略用于创建用户和数据库的自定义脚本。 I've tried on many ways. 我试过很多方法。 Here is one of my configuration. 这是我的配置之一。

Solution 1 解决方案1

docker-compose.yml (version 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

networks: mynet: 网络:mynet:

dockerfile dockerfile

FROM mongo:latest
COPY ./data/mongo-init/mongo-init.sh /docker-entrypoint.d/

Solution 2 解决方案2

docker-compose.yml (version 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

networks: mynet: 网络:mynet:

dockerfile dockerfile

FROM mongo:latest

Here is my init script: 这是我的init脚本:

mongo-init.sh 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"

Does anyone have an idea how to run initialization script. 有谁知道如何运行初始化脚本。

Thank you in advance. 先感谢您。

The problem was that I have used ./data/mongo-init:/docker-entrypoint.d/ instead of ./data/mongo-init:/docker-entrypoint-initdb.d/ . 问题是我使用了./data/mongo-init:/docker-entrypoint.d/而不是./data/mongo-init:/docker-entrypoint-initdb.d/

I have changed that and it works like charm. 我改变了它,它就像魅力一样。

PS Instead of sh script is always better to use pure js. PS而不是sh脚本总是更好地使用纯js。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM