MONGO_INITDB_DATABASE value is ignored

I have a docker-compose.yml file with below config.

version: '0.01'
services:
  mongodb:
    image: mongodb/mongodb-community-server:latest
    container_name: test-mongodb
    restart: always
    ports:
      - "27017:27017"
    environment:
      MONGO_INITDB_ROOT_USERNAME: testadmin
      MONGO_INITDB_ROOT_PASSWORD: testadmin
      MONGO_INITDB_DATABASE: "peopledb"
    volumes:
      - ./setup/mongo-init-scripts:/docker-entrypoint-initdb.d

and the init-db.js as below

db.createUser({
    user: "admin",
    pwd: "admin_password",
    roles: [
      {
        role: "readWrite",
        db: "peopledb"
      }
    ]
  });

  db.people.insertMany([
    { name: "first 1", value: 10 },
    { name: "second 2", value: 20 },
    // Add more default documents as needed
  ]);

Everything works fine without a hitch, except the people collection is created in “Test” db instead of “peopledb” in the docker-compose and init-js.

What am I missing…

TIA

Hi @gajakannan ,
add this command above insertMany line
use peopledb;
in init-db.js file

Hi @Pavan_Vinnakota ,

I am getting the below error when I did “use peopledb;”

So, this :point_down:

use peopledb

should be :point_down:

conn = new Mongo();
db = conn.getDB("peopledb");

Meant to say in my last reply, the issue is fixed by using conn instead of use…

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.