mguay22 / nestjs-rabbitmq-microservices

392 stars 202 forks source link

App not starting with a mongoDB error #4

Open PhilippeCorreges opened 5 months ago

PhilippeCorreges commented 5 months ago

After Full install and a npm start dev, getting the following error message. 3 docker mongoDB containers are successfully started.

[Nest] 3492 - 21/05/2024 15:49:53 ERROR [MongooseModule] Unable to connect to the database. Retrying (1)...

dbosnjak94 commented 1 month ago

It looks like the issue you're encountering is related to the connection between your NestJS application and the MongoDB containers. I had a similar issue, and switching to the official MongoDB image and adjusting the Docker Compose setup fixed it for me.

Here’s what worked:

1) If you're using the Bitnami MongoDB image, try switching to the official MongoDB image from Docker Hub. Here's how you can configure it in your docker-compose.yml:

services:
  mongo:
    image: mongo
    restart: always
    environment:
      MONGO_INITDB_ROOT_USERNAME: root
      MONGO_INITDB_ROOT_PASSWORD: example
    ports:
      - '27017:27017'

2) Update the MongoDB connection URI: In your .env file (for each microservice), update the MONGODB_URI to point to the correct MongoDB instance. For example:

MONGODB_URI=mongodb://root:example@mongo:27017/mydatabase?authSource=admin

Replace mydatabase with the name of your actual database. The authSource=admin parameter ensures MongoDB uses the admin database for authentication.

3) Make sure each of your microservices depends on MongoDB in the docker-compose.yml. For example, if you're running a NestJS microservice like auth, ensure it has a depends_on entry for the MongoDB service:

depends_on:
  - mongo

4) After making these changes, rebuild and restart your Docker containers by running:

docker-compose down
docker-compose up --build -V