Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example to documentation for "Initializing a fresh instance" #663

Open
melvingaye opened this issue Jan 16, 2024 · 6 comments
Open

Comments

@melvingaye
Copy link

Problem

Setting up a user and db (other than the admin user and test db) is a little opaque.

This link How to create a DB for MongoDB container on start up? with over 268K views shows how much of an issue the lack of documentation is. There are many other variations of the search term above, so I'd imagine the numbers would be a lot higher if they were all tallied.

Proposal

Please include something like the below in that section of the documentation

docker-compose.yaml

version: '3.8'

services:
    mongodb:
        image: mongo:latest
        container_name: mongodb
        restart: always
        environment:
            MONGO_INITDB_ROOT_USERNAME: <admin user>
            MONGO_INITDB_ROOT_PASSWORD: <admin password>
            MONGO_INITDB_DATABASE: <custom db>
        ports:
            - 27017:27017
        volumes:
            - ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro

mongo-init.js

print('Init Script - Started Adding the User Roles.');
// use the admin db to create a new user and give that new user permission to the db
// mongo seems to lazy load the db, meaning it'll create the db when a collection is first added
// but the newly created user will still have access to the db
db = db.getSiblingDB('admin');
db.createUser({
  user: '<custom user>',
  pwd: '<custom user password>',
  roles: [{ role: 'readWrite', db: '<custom db>' }],
});
print('Init Script - Ended Adding the User Roles.');
please include the shell version
@LaurentGoderre
Copy link
Member

@Spucis
Copy link

Spucis commented Aug 30, 2024

@melvingaye so its mandatory to do the db.getSibilings()? I don't understand it, if i have to specify the custom db in the script, what's the point to use the MONGO_INITDB_DATABASE environment variable in the first place?

Thanks in advance!

@drone1
Copy link

drone1 commented Jan 27, 2025

I'm losing my mind here.

Is there something I need to do to ensure that the root user is created on first startup?

Yes, I've read the docs.

I docker 'down', delete all volumes, all containers, even images, prune system -af – I have even reinstalled Docker, and I still cannot get mongo to create the root user in the auth database on startup.

run-servers.yml:

  mongodb:
    image: mongo:7.0.16
    restart: always
    ports:
      - 27017:27017
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: password           # I've also tried with "password"

${PROJECT_ROOT}/dev.mongodb.conf:

net:
  port: 27017
  bindIp: 0.0.0.0  # Listen to all IP addresses

security:
  authorization: "enabled"

docker-compose -f run-servers.yml up

Servers start. Mongo doesn't print anything about a root user being created.

Does anyone know what I'm doing wrong here?

mongosh localhost --port 27017 -u root -p password --authenticationDatabase

Nope, authentication failed.

I would really appreciate some help on this. I just lost my 3rd day trying to figure this out.

Thank you.

PS: I'm on macOS Sequoia 15.2, and the issue appears to yield the same result with both mongo:7.0.16 and arm64v8/mongo:7.0.16 images

@LaurentGoderre
Copy link
Member

@drone1 from your example, the config file doesn't seem to be added to the container.

@LaurentGoderre
Copy link
Member

Even without the config file it's working for me though.

Here is it working by creating an ephemeral container to connect to the mongo instance

docker run --rm -it mongo:7.0.16 mongosh --host host.docker.internal --port 27017 -u root -p password --authenticationDatabase

@drone1
Copy link

drone1 commented Jan 27, 2025

I just figured it out actually:

mongosh localhost --port 27017 -u root -p password --authenticationDatabase

this is wrong. When you use --authenticationDatabase, you need to specify a database name (admin).

So this was the root of all my issues here. No pun intended.

Also: kill me.

Also: thanks a lot for getting back here so quickly.

Oh hmm, you don't specify a db name for --authenticationDatabase either? Now I'm even more confused.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants