Unable to connect to a MongoDB replicaset from a NestJS app all hosted on the same container

Hi everyone,

I am new to docker so this could be a noob issue.
I’ve been trying to follow this tutorial and using an M1 Mac and bitnami MongoDB docker image:

The main issue which I have wasted several days on:

Here is the docker-compose file:

networks:
    app-tier:
      driver: bridge
  services:
    orders:
      build:
        context: .
        dockerfile: ./apps/orders/Dockerfile
        target: development
      command: npm run start:dev orders
      env_file:
        - ./apps/orders/.env
      depends_on:
        - mongodb-primary
        - mongodb-secondary
        - mongodb-arbiter
        # - billing
        # - auth
        - rabbitmq
      volumes:
        - .:/usr/src/app
        - /usr/src/app/node_modules
      networks:
        - app-tier
      ports:
        - '3000:3000'
    rabbitmq:
      image: rabbitmq
      ports:
        - '5672:5672'
    mongodb-primary:
      networks:
        - app-tier
      image: docker.io/bitnami/mongodb:5.0
      environment:
        - MONGODB_ADVERTISED_HOSTNAME=mongodb-primary
        - MONGODB_REPLICA_SET_MODE=primary
        - MONGO_INITDB_ROOT_USERNAME=username123
        - MONGODB_ROOT_PASSWORD=password123
        - MONGODB_REPLICA_SET_KEY=replicasetkey123
      volumes:
        - 'mongodb_master_data:/bitnami/mongodb'
      ports:
        - '27017:27017'
  
    mongodb-secondary:
      networks:
        - app-tier
      image: docker.io/bitnami/mongodb:5.0
      depends_on:
        - mongodb-primary
      environment:
        - MONGODB_ADVERTISED_HOSTNAME=mongodb-secondary
        - MONGODB_REPLICA_SET_MODE=secondary
        - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
        - MONGO_INITDB_ROOT_USERNAME=username123
        - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123
        - MONGODB_REPLICA_SET_KEY=replicasetkey123
  
    mongodb-arbiter:
      networks:
        - app-tier
      image: docker.io/bitnami/mongodb:5.0
      depends_on:
        - mongodb-primary
      environment:
        - MONGODB_ADVERTISED_HOSTNAME=mongodb-arbiter
        - MONGODB_REPLICA_SET_MODE=arbiter
        - MONGODB_INITIAL_PRIMARY_HOST=mongodb-primary
        - MONGO_INITDB_ROOT_USERNAME=username123
        - MONGODB_INITIAL_PRIMARY_ROOT_PASSWORD=password123
        - MONGODB_REPLICA_SET_KEY=replicasetkey123
  
  volumes:
    mongodb_master_data:
      driver: local

And below is the nestjs connection:


@Module({
  imports: [
    MongooseModule.forRootAsync({
      useFactory: (configService: ConfigService) => {
        return {
          uri: 'mongodb://username123:password123@mongodb-primary:27017/',
          useNewUrlParser: true,
          useUnifiedTopology: true,
        };
      },

      inject: [ConfigService],
    }),
  ],
})

I’m getting the following error:MongooseServerSelectionError: connect ECONNREFUSED 172...*:27017

I edited your post because it wasn’t clear thaty ou shared two different links.

All I see is that your compose file is syntactically incorrect, but that can’t be the problem because that you would have noticed.

Why do you need the network definition? You specified apptier network for both services. Compose will create it for you. I remember another issue where using a custom network broke the app. I don’t know why, but you could still try without the custom network.