1

I have this docker-compose file:

version: "3"
services:
  mongo:
    image: bitnami/mongodb:4.4
    restart: always
    ports:
      - 27017:27017
    volumes:
      - ./db:/bitnami/mongodb
    environment:
        - MONGODB_REPLICA_SET_MODE=primary
        - ALLOW_EMPTY_PASSWORD=yes

which should expose the MongoDB replica set to the outer host via mongodb://localhost:27017 . However I am getting this error message:

Server selection timeout: None of the available servers suitable for criteria Predicate. Topology: { Type: ReplicaSetNoPrimary, Servers: [ { Address: mongo:27017, Type: RsGhost, Average RTT: 10.349064ms, Last Update Time: DateTime(OffsetDateTime { utc_datetime: PrimitiveDateTime { date: Date { year: 2022, ordinal: 197 }, time: Time { hour: 21, minute: 14, second: 38, nanosecond: 986000000 } }, offset: UtcOffset { hours: 0, minutes: 0, seconds: 0 } }), Max Wire Version: 9, Min Wire Version: 0 }, ] }

How do I expose the replica set to the outer host? I've tried adding 127.0.0.1 mongo to my /etc/hosts with no luck. I am on macOS 12.4 and Docker Desktop 4.10.1.

tyteen4a03
  • 1,812
  • 24
  • 45

1 Answers1

0

Localhost (127.0.0.1) is your problem. Only clients what are in side that mongod -pod can connect, but as we know, in that pod there is only mongodb and client(s) are in the different pods.

You need to use "Docker container networking"

Create a network ($ docker network create app-tier --driver bridge) and then configure your mongodb (compose -file) to use it:

networks:
  app-tier:
    driver: bridge

services:
  mongodb:
    image: 'bitnami/mongodb:4.4'
    networks:
      - app-tier
JJussi
  • 1,540
  • 12
  • 12