0

I'm currently creating a database and a python file that controls it. The issue i'm having is that it runs but the creation of the user seems to not be happening post the docker-compose up command. My knowledge on users in Mongo is limited but realistically I only need one user for now with most of the rights. My docker-compose file looks like this:

version: '3'
services:
   storing_script:
     build:
      context: .
      dockerfile: Dockerfile
    depends_on:
      - mongo

mongo:
  image: mongo:latest
  environment:
        MONGO_INITDB_ROOT_USERNAME: user
        MONGO_INITDB_ROOT_PASSWORD: pas342%4@!
        MONGO_INITDB_DATABASE: motionDB
volumes:
- ${PWD}/mongo-data:/data/db
- ./mongo-init.js:/docker-entrypoint-initdb.d/mongo-init.js:ro
command: mongod --port 27018
restart: unless-stopped

The storing script has its own DockerFile. I've connected to the mongo container and it does run, it does work but it seems to completely ignore the creation of a user. I specified in the volumes to use the js file for the creation which looks like:

db.createUser(
    {
        user: "<user for database which shall be created>",
        pwd: "<password of user>",
        roles: [
            {
                role: "readWrite",
                db: "<database to create>"
            }
        ]
    }
);

Is my compose file the issue, or do I need to manually create and authenticate a user? I looked at other topics and they all seem to recommend this. A specific topic said when running docker-compose up I should receive a "user created" (https://stackoverflow.com/a/42973849/15718345) which i never receive. Again i'm not a Mongo expert, my goal is just to get a user working as soon as possible.

ajay1738
  • 25
  • 5

1 Answers1

0

Apparently it had been storing, When accessing the container you have to authenticate.

mongo --port 27018 --authenticationDatabase "admin" -u "user" -p "pas342%4@!"

Then you are able to see the database and collection, again im not a mongo expert so I dont fully understand why I had to connect to it with authentication to see the database. If anyone knows it would be appreciated :)

ajay1738
  • 25
  • 5