Docker: Start single node "Replica Set" MongoDB

YURII DE. - Jul 24 '23 - - Dev Community

The example below is for development purposes. Never use it in production.

Tested

MongoDB: 6.0.8
Docker: 4.21.1 (114176)

version: '3.4'

services:
  mongodb:
    image: mongo
    hostname: mongodb
    restart: always
    container_name: mongodb
    ports:
      - 27017:27017
    networks:
      - mongo-network
    healthcheck:
      test: test $$(echo "rs.initiate().ok || rs.status().ok" | mongo -u mongo -p mongo --quiet) -eq 1
      interval: 10s
      start_period: 30s
    # trunk-ignore(yamllint/quoted-strings)
    command: ["--replSet", "dbrs", "--bind_ip_all"]

networks:
  mongo-network:
    driver: bridge
Enter fullscreen mode Exit fullscreen mode

Connecting string

mongodb://127.0.0.1:27017/?directConnection=true&serverSelectionTimeoutMS=2000

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .