1

docker-compose.yml

version: '3.7'

services:
 mongodb:
    image: mongo:latest
    container_name: mongodb
    restart: always
    environment:
        MONGO_INITDB_ROOT_USERNAME: devuser
        MONGO_INITDB_ROOT_PASSWORD: devuser
        MONGO_INITDB_DATABASE: devDB
    ports:
        - 27017:27017
    volumes:
        - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js

 db.createUser(
    {
        user: "devuser",
        pwd: "devuser",
        roles: [
            {
                role: "readWrite",
                db: "devDB"
            }
        ]
    }
 );
use devDB;
db.item.insert(
 [
   {
     "id": "xsd4cv89t5f5um4b",
     "name": "r44d7piq",
     "price": 1485521573482,
     "dateCreation": 1485521569056
   }  
 ]

);

I run it

 docker-compose up --build -d mongodb

I connect to mongodb

mongo -u devuser -p devuser

I check dbs:

> show dbs
admin   0.000GB
config  0.000GB   
local   0.000GB

Why I don't see the devDB?

It seems to me pretty standard, I've copied the above from several examples...

1 Answer 1

1

The database is created on demand when an operation that needs it is performed.

You haven't performed any operations, hence there is no database created.

Sign up to request clarification or add additional context in comments.

5 Comments

I've added an insert to the script (see edited question), but nothing is happening...
I've added also "use devDB" and db.insert.item...but still I don't see anything
Is the script being run?
good question...I don't see it running in the logs...so why?
I tried to delete all previous containers and mount a data volume. it seems the user is created but not the db. Don't understand why

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.