Closed redabourial closed 11 months ago
I experienced a similar issue. Not sure if it's your case.
I'm working with the mongo:latest
image and a docker-compose.yml
file, without a volume mount. At the beginning, I hadn't set a password for development.
mongo:
image: mongo:latest
networks:
- backend
But I subsequently added one to my setup. then use docker compose up -d
to recreate my container
mongo:
image: mongo:latest
networks:
- backend
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
I can see the environment variables were set inside the container
Even downgrading from latest to 7.0 or 6.0, the problem didn't go away, and the latter even led to a compatibility error. It give me a hint that some persistent data are left behind.
When I remove the container with docker rm -f <container>
and bring it up again, everything works fine.
@redabourial there are two issues with the code.
First the env vars should be MONGO_INITDB_ROOT_USERNAME
and MONGODB_INITDB_ROOT_PASSWORD
(Note the MONGO_
not MONGODB_
)
~The second one is the volume. Commenting out the - ./db/mongo:/data/db
makes it work.~ It also fails if the folder db/mongo
doesn't exist on the host in the same folder as the Dockerfile
@LaurentGoderre thanks for the insight, i can connect to the admin database though (so for development purposes my issue is solved). I still don't understand why MONGO_INITDB_DATABASE doesn't have any effect. Do you have any idea why that is ?
@redabourial you might need to modify your connection string:
mongodb://root:ToHTsg3eChI2oSyTEpNuWwDOT8woSljN@mongo:27017/project?authSource=admin
From the docs on Docker Hub:
MongoDB is fundamentally designed for "create on first use", so if you do not insert data with your JavaScript files, then no database is created.
If you don't insert anything, then no database is created.
The user created by MONGO_INITDB_ROOT_USERNAME
and MONGO_INITDB_ROOT_PASSWORD
is in the admin
database, so that is where authentication must be directed: like authSource
or --authenticationDatabase
Here is my docker-compose file :
my .env \:
mongo-express logs :
and mongo db logs :
I tried connecting to the database with studio 3t with no success.