Skip to content

nesvet/mongo-rs

MongoDB with replica set — Docker image

CI Docker Image Docker Pulls license

Your app needs a replica set — Node.js driver, Mongoose, Change Streams, transactions all require it. The official mongo image runs standalone; mongo-rs extends it with a replica set on first run. One container, no init scripts.

docker run -d -e MONGO_PASSWORD=secret nesvet/mongo-rs

Docker Compose

docker compose up -d

Example with app service:

services:
  mongo:
    image: nesvet/mongo-rs
    environment:
      MONGO_PASSWORD: ${MONGO_PASSWORD:-secret}
    volumes:
      - mongo-data:/data/db
    restart: unless-stopped

  app:
    image: myapp:latest
    environment:
      MONGO_URI: mongodb://root:${MONGO_PASSWORD:-secret}@mongo:27017/?directConnection=true
    depends_on:
      mongo:
        condition: service_healthy

volumes:
  mongo-data:

Features

  • Auto-init replica setrs.initiate() on first run, no manual setup
  • Built-in healthcheck — 15s interval, ready for depends_on: condition: service_healthy
  • Env-based configMONGO_RS, MONGO_USER, MONGO_PASSWORD, MONGO_AUTHDB
  • Secrets supportMONGO_PASSWORD_FILE for Docker secrets / Kubernetes
  • FCV auto-updatefeatureCompatibilityVersion aligned with MongoDB version

Use cases

→ Local dev with Node.js / Mongoose
No more MongoServerSelectionError: connection refused. Your driver expects a replica set; mongo-rs provides one.

→ Docker Compose with depends_on
Healthcheck built in. depends_on works out of the box.

→ Same setup dev → prod
Single node locally, add nodes in production. Same connection string format, no code changes.

Usage

Variable Default Description
MONGO_RS rs0 Replica set name
MONGO_AUTHDB admin Authentication database
MONGO_USER root Admin username
MONGO_PASSWORD iddqd Admin password
MONGO_PASSWORD_FILE Path to file with password (Docker secrets)

Connection string (single-node, use directConnection=true): options

mongodb://root:${MONGO_PASSWORD}@mongo:27017/?directConnection=true

Production

  • Change default passwordiddqd is for dev only

  • Use secretsMONGO_PASSWORD_FILE with Docker secrets or an external secret manager:

    services:
      mongo:
        image: nesvet/mongo-rs
        environment:
          MONGO_PASSWORD_FILE: /run/secrets/mongo_password
        secrets:
          - mongo_password
        volumes:
          - mongo-data:/data/db
        restart: unless-stopped
    
    secrets:
      mongo_password:
        file: ./secrets/mongo_password.txt
    
    volumes:
      mongo-data:
  • See SECURITY.md for vulnerability reporting

Testing

./test.sh

Support this project

mongo-rs is free, open-source, and maintained by one developer.

If it saves you time or improves your MongoDB replica set setup:

  • ⭐ Star the repo — it helps discoverability
  • 💙 Support on Patreon — priority features & long-term maintenance

Contributing

See CONTRIBUTING.md

License

MIT

About

Docker image: MongoDB with replica set out of the box. For Node.js, Mongoose, Change Streams, transactions. One container, no init scripts.

Topics

Resources

License

Code of conduct

Contributing

Security policy

Stars

Watchers

Forks

Sponsor this project

Packages

 
 
 

Contributors