I am facing a weird problem regarding the mongo container. I have a server machine whose IP address is 17.17.17.17. In that machine I am trying to deploy my MongoDB, and I want it to be accessible from other machines. I have used the following command to deploy mongo and allowed the port 27017 in the firewall.
docker run -d \
--name mongo \
-p 27017:27017 \
-v /path/to/mongo:/data/db \
-e MONGO_INITDB_ROOT_USERNAME=root \
-e MONGO_INITDB_ROOT_PASSWORD=password \
mongo:latest
The mongo container is running but when I try to access it from another machine either via mongosh or via a nodejs process (mongoose library) it is not accessible.
I am using the following command to access mongodb deployed in this machine from other machine:
mongosh --host 17.17.17.17. --port 27017 --username root --password password --authenticationDatabase admin
This shows authentication error every time and in the server machine logs. It says it is not getting any user named root in the admin database.
In the nodejs , I am using the following urls to connect to the mongodb:
mongodb://root:password@17.17.17.1:27017/test?authSource=admin
mongodb://root:password@17.17.17.1:27017/admin?authSource=admin
Again, it fails to connect to the mongodb.
However, if I do not use any kind of authentication to deploy mongo db, it can be accessed both via mongosh and nodejs.
For example if I use this:
docker run -d \
--name mongo \
-p 27017:27017 \
-v /path/to/mongo:/data/db \
mongo:latest
It is accessible both via mongosh and nodejs library from other machines.
Can anyone tell me what am I doing wrong here?