0

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.

Heril Muratovic
  • 1,940
  • 6
  • 25
  • 46

1 Answers1

0

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.

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

Heril Muratovic
  • 1,940
  • 6
  • 25
  • 46