Problem with mongo docker image and mongoose as Object Document mapper to connect to specific db after creating container:
(I made sure that DB_NAME is not undefined...)
await mongoose.connect(`${process.env.MONGO_URI}/${process.env.DB_NAME}`, {
Idk why, but whenever I try to connect to mongodb in docker container (with docker-compose built) I get this error:
error: Failed to establish MongoDB connection: MongoServerError: Authentication failed..
But when I try to connect with MongoDB Atlas without the db name just the URI, i can easily connect...
My Docker-compose file:
version: "3"
services:
server:
build:
context: ./server
dockerfile: ./Dockerfile
ports:
- 3001:3001
volumes:
- ./server/dist:/home/nodeUser/app/dist
depends_on:
- mongodb
mongodb:
image: mongo
restart: always
environment:
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: toor
MONGO_INITDB_DATABASE: auth
ports:
- 27017:27017
MY .env file:
PORT=3001
MONGO_URI=mongodb://root:toor@mongodb:27017/auth
DB_NAME=auth
Weirdly enough is, when I omit the credentials, so no Authentication required to connect, then it works...
I tried to omit the credentials -- login without auth required: worked I tried to connect using the same connection uri but without the db name: worked I checked if db_name could be undefined in process.env.DB_NAME: negative