10

I am using following docker-compose file

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

Here is my initdb.js file

db.createUser(
{
  user: "api-test",
  pwd: "api-test",
  roles: [
    {
      role: "readWrite",
      db: "api-test"
    }
  ]
}
);

I can see only 3 databases and unable to connect to api-test DB

enter image description here

Please help me if something is missing

gregorycallea
  • 1,218
  • 1
  • 9
  • 28
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
  • You don't have `api-test` database. Do You need create first? – Valijon Mar 04 '20 at 09:13
  • @Valijon I am trying to achieve whenever i run docker-compose up command it should create mongob image in the docker with api-test database and user – Munish Kapoor Mar 04 '20 at 09:15
  • Try to change `MONGO_INITDB_DATABASE: root-db` to `MONGO_INITDB_DATABASE: api-test`. [source](https://stackoverflow.com/questions/42912755/how-to-create-a-db-for-mongodb-container-on-start-up) – Valijon Mar 04 '20 at 09:17
  • i have tried also dosn't work even i can't see the root-db in MongoDB compass – Munish Kapoor Mar 04 '20 at 09:18
  • not even [this](https://stackoverflow.com/a/52944387/3710490) ? Try to enter inside docker image and run manually, maybe you got some permission issue – Valijon Mar 04 '20 at 09:20
  • nope, I am trying using docker-compose file that one is Docker file – Munish Kapoor Mar 04 '20 at 09:25

2 Answers2

24

Able to solve the issue with the help of the following article Docker-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 #################################################################');
Munish Kapoor
  • 3,141
  • 2
  • 26
  • 41
2

Although my setup was correct, removed volumes, modified the db, etc, none of these worked for me.

The only way was to change the name of the mongodb container inside the docker-compose, instead of mongo I wrote mongodb and it worked.

Ali H. Kudeir
  • 756
  • 2
  • 9
  • 19