MongoDB authentication fails in Docker - mongodb

My docker-compose.yml
mongo:
image: mongo
volumes:
- ./mongo:/data/db
ports:
- 27017:27017
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: pass
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: pass
ME_CONFIG_MONGODB_URL: mongodb://root:pass#mongo:27017/
When I run docker-compose up it logs the following errors
for the mongo service:
UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
and for the mongo-express service:
"error":"UserNotFound: Could not find user "root" for db "admin"
I tried adding ?authSource=admin to the ME_CONFIG_MONGODB_URL but the error remains.

Related

Getting "MongoServerError: Authentication failed" when connecting to MongoDB in Docker container

Here is my docker-compose file:
version: '3' services: mongo:
image: mongo
container_name: mongo
restart: always
environment:
- MONGO_INITDB_ROOT_USERNAME: admin
- MONGO_INITDB_ROOT_PASSWORD: root
ports:
- 27017:27017
volumes:
- ./mongo-data:/data/db
command: --wiredTigerCacheSizeGB 1.5
I try to connect to Mongo container but constantly receive the same error:
MongoServerError: Authentication failed
Here are 2 variants of connection strings I've used, neither worked:
mongodb://admin:root#localhost:27017/admin
mongodb://admin:root#localhost:27017/admin?authSource=admin
What can I try next?

MongoError: Authentication failed with docker-compose

I am trying to build this app https://github.com/dwgebler/node-express-example. I want to use it as a template for making a Mongodb http express server. I tried to build the docker compose file in this repo which looks like this (modified slightly):
version: "3.8"
services:
database:
image: mongo
container_name: mongo-database
restart: always
volumes:
- "mongodata:/data/db"
ports:
- "27017:27017"
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
container_name: mongo-express
restart: always
ports:
- "8081:8081"
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_SERVER: mongo-database
web:
image: node:16-alpine
container_name: node-server
restart: always
user: "node"
ports:
- "9443:443"
volumes:
- "./app:/var/app/"
working_dir: "/var/app"
environment:
NODE_ENV: dev
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
command: "./wait.sh mongo-database 27017 'npm start'"
volumes:
mongodata:
But when I run the command docker compose up -d in the same directory as the docker-compose.yml file, I get this in the "node-server" container logs:
Mongo started
MongoError: Authentication failed.
at MessageStream.messageHandler (/var/app/node_modules/mongodb/lib/cmap/connection.js:272:20)
at MessageStream.emit (node:events:513:28)
at processIncomingData (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:144:12)
at MessageStream._write (/var/app/node_modules/mongodb/lib/cmap/message_stream.js:42:5)
at writeOrBuffer (node:internal/streams/writable:391:12)
at _write (node:internal/streams/writable:332:10)
at MessageStream.Writable.write (node:internal/streams/writable:336:10)
at Socket.ondata (node:internal/streams/readable:754:22)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:315:12) {
ok: 0,
code: 18,
codeName: 'AuthenticationFailed'
}
Why am I getting a MongoError: Authentication failed? Notice that I set
DB_USER: root
DB_PASSWORD: example
DB_NAME: mongo-database
In the web section of the docker file which matches the MONGO_INITDB_ROOT_USERNAME, MONGO_INITDB_ROOT_PASSWORD, from the database section and I set DB_NAME to mongo-database which matches the database section name. So why isn't it authenticating?
MONGO_INITDB_ROOT_USERNAME and PASSWORD create a user in the Mongo admin database. Your connection string tries to authenticate against the mongo-database database and it can't find the user. There are messages in the Mongo logs that tell you this.
To fix it, you need to authenticate against the admin database by adding authSource=admin to your connection string, like this
const uri = `mongodb://${process.env.DB_USER}:${process.env.DB_PASSWORD}#mongo-database/${process.env.DB_NAME}?retryWrites=true&writeConcern=majority&authSource=admin`;

docker-compose service name problem using mongo official image

I followed official mongo image description trying to using it with docker-compose.But i've noticed that the mongo service name can not be the same as the image name.Here is the situation:
created a docker-compose.yml file which filled with this:
version: '3.1'
services:
mongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#mongo:27017/
execute docker-compose
docker-compose -f docker-compose.yml up
Then the error appears:
Could not connect to database using connectionString: mongodb://root:example#mongo:27017/"
mongo-express_1 | (node:8) UnhandledPromiseRejectionWarning: MongoNetworkError: failed to connect to server [mongo:27017] on first connect [MongoError: Authentication failed.
mongo-express_1 | at Connection.messageHandler (/node_modules/mongodb/lib/core/connection/connection.js:364:19)
mongo-express_1 | at Connection.emit (events.js:314:20)
But when I change the service name mongo to myMongo,it just work out!:
version: '3.1'
services:
myMongo:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
mongo-express:
image: mongo-express
restart: always
ports:
- 8081:8081
environment:
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example#myMongo:27017/
So, where am i wrong?

Can't connect to MongoDB inside docker container

I'm trying to connect to a mongodb container inside docker but I'm getting this error:
getaddrinfo ENOTFOUND mongo.ks.local
The database works just fine when I access it from docker but I'm not able to access it from MongoDBCompass
This is my docker-compose.yml file:
version: '3.8'
services:
mongo:
image: mongo
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: <username>
MONGO_INITDB_ROOT_PASSWORD: <password>
ports:
- 27017:27017
volumes:
- ks_mongodb:/data/db
I'm quite new to docker and will appreciate any help I can get
You need the ip address of the container:
try:
docker inspect <CONTAINER NAME>
And search for the ip address. Then use the ip address to connect to mongodb
I have tried connecting to the container using mongo compass. It failed but when I removed the line of mounting the volume and tried again it connects to the database.
Then tried to mount the volume like below it worked. I have changed the username and password and specified the INITDB env variable.
version: "3.8"
services:
mongo:
image: mongo:latest
container_name: mongo.ks.local
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: 1234
MONGO_INITDB_DATABASE: admin
ports:
- 27017:27017
volumes:
- /data/db

connect EHOSTUNREACH 172.24.0.2:27017 docker-compose

I am currently unable to connect to mongo when I run docker-compose, I keep on getting the error below
connect EHOSTUNREACH 172.25.0.2:27017
Here is my docker-compose.yml file
version: "3.4"
services:
app:
container_name: checki
restart: always
build:
context: .
network: host
ports:
- "3000:9000"
links:
- mongo
mongo:
container_name: mongo
image: mongo
volumes:
- ./data:/data/db
ports:
- "27018:27017"
and my db connection string is like this
"mongodb://mongo:27017/checki"
Please assist