Docker Compose MongoDB docker-entrypoint-initdb.d is not working Docker Compose MongoDB docker-entrypoint-initdb.d is not working docker docker

Docker Compose MongoDB docker-entrypoint-initdb.d is not working


Able to solve the issue with the help of the following articleDocker-Compose mongoDB Prod, Dev, Test Environment

The file structure should be

Project├── docker-compose.yml (File)├── docker-entrypoint-initdb.d (Directory)│   ├── mongo-init.js (File)

docker-compose.yml file

version: '3.7'services:   mongo:    container_name: container-mongodb    image: mongo:latest    restart: always    ports:      - 27017:27017    environment:      MONGO_INITDB_ROOT_USERNAME: root      MONGO_INITDB_ROOT_PASSWORD: password      MONGO_INITDB_DATABASE: root-db    volumes:      - ./docker-entrypoint-initdb.d/mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js file

print('Start #################################################################');db = db.getSiblingDB('api_prod_db');db.createUser(  {    user: 'api_user',    pwd: 'api1234',    roles: [{ role: 'readWrite', db: 'api_prod_db' }],  },);db.createCollection('users');db = db.getSiblingDB('api_dev_db');db.createUser(  {    user: 'api_user',    pwd: 'api1234',    roles: [{ role: 'readWrite', db: 'api_dev_db' }],  },);db.createCollection('users');db = db.getSiblingDB('api_test_db');db.createUser(  {    user: 'api_user',    pwd: 'api1234',    roles: [{ role: 'readWrite', db: 'api_test_db' }],  },);db.createCollection('users');print('END #################################################################');