I have a project with docker containers:
And NestJS doesn't want to connect to mongoDB but connects to mongoDB Atlas. Tried everything nothing helps(
docker-compose.yml:
api:
image: service-api
build:
context: "api/."
dockerfile: "Dockerfile"
environment:
NODE_ENV: production
ME_CONFIG_MONGODB_ADMINUSERNAME: root
ME_CONFIG_MONGODB_ADMINPASSWORD: example
ME_CONFIG_MONGODB_URL: mongodb://root:example@mongo:27017/eshop
networks:
- backend
- frontend
depends_on:
- 'mongo'
- 'rabbitmq'
ports:
- "8802:8802"
deploy:
placement:
constraints: [ node.role == manager ]
replicas: 1
...
mongo:
image: mongo:6.0.2
networks:
- backend
ports:
- "27017:27017"
environment:
MONGO_INITDB_DATABASE: eshop
MONGO_INITDB_ROOT_USERNAME: root
MONGO_INITDB_ROOT_PASSWORD: example
volumes:
- "/opt/mongo/data/db:/data/db"
deploy:
placement:
constraints: [ node.role == manager ]
replicas: 1
NestJS app.module.ts:
const MongoModule = MongooseModule.forRoot('mongodb://root:example@mongo:27017/eshop', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});
mongoose version: ^5.8.11
Error message:
[MongooseModule] Unable to connect to the database. Retrying (1)... +29999ms
[MongooseModule] Unable to connect to the database. Retrying (2)... +33002ms
...
[MongooseModule] Unable to connect to the database. Retrying (7)... +33002ms
Everything works in same stack, in same network, and still can't connect
set authSource option to admin
const MongoModule = MongooseModule.forRoot('mongodb://root:example@mongo:27017/eshop?authSource=admin', {
useNewUrlParser: true,
useUnifiedTopology: true,
useFindAndModify: false,
});