简体   繁体   中英

MongoDB docker-entrypoint issue

MongoDB docker-entrypoint ignoring custom script for creating user and database. I've tried on many ways. Here is one of my configuration.

Solution 1

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

dockerfile

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

Solution 2

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

dockerfile

FROM mongo:latest

Here is my init script:

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/ .

I have changed that and it works like charm.

PS Instead of sh script is always better to use pure js.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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