MongoDB MongoDB

Popular NoSQL document database

MongoDB is a source-available, document-oriented NoSQL database designed for scalability and developer agility, storing data as flexible, JSON-like documents rather than rows and columns.

This is a shared instance, following the same pattern as postgresql/couchdb in this category. Mongo’s /docker-entrypoint-initdb.d scripts only run once, on first startup with an empty data directory — each consuming service should add its own init script under ./config/initdb when it first starts using this instance. A service added after this instance already has data must create its user/db manually via mongosh instead, since init scripts won’t re-run.

TODO: Add a mongo-express (or similar) admin UI service if a web GUI is wanted

References

Backup

backup.yaml creates nightly full-instance mongodump --archive --gzip exports under ${DB_BACKUP_ROOT}/database-mongodb/. The standalone server’s known writer, UniFi, is briefly stopped and restarted even on failure. Restore all databases/users with paired UniFi config and original credentials. See the backup guide for commands, retention and snapshot selection.

Recovery

  1. Recover database.archive.gz and its manifest; verify the hash and compressed payload before restoring into a compatible MongoDB destination.
  2. Keep application writers stopped and use mongorestore with --archive and --gzip to recover the full-instance databases/users/roles with original credentials. Start MongoDB before the controller.
  3. Restore UniFi’s paired configuration and controller settings before reconnecting devices.

Standalone MongoDB has no --oplog capture. Consistency assumes UniFi is the only writer; reassess that assumption when adding consumers. Normal cleanup restarts UniFi, but a killed job or host interruption can bypass it: check/start UniFi manually if it remains stopped.

name: mongodb
services:
  mongodb-init:
    image: busybox:1.38.0@sha256:fd7dc98638c8e305f4dc34e979f1c0fdfdcaeb0fbf8fcff77ae834b6da3d7e6e
    container_name: mongodb-init
    command: ["sh", "-c", "chown -R 999:999 /data/db"]
    volumes:
      - ${DOCKER_VOLUMES}/mongodb/data:/data/db

  mongodb:
    image: mongo:7.0.45-jammy@sha256:7fba8c37d681a79f22a85d2b06bd5ed121a7973c0a6ca20ed4887bb0a1548ce0
    container_name: mongodb
    restart: unless-stopped
    # kics-scan ignore-block
    environment:
      MONGO_INITDB_ROOT_USERNAME: ${MONGO_ROOT_USERNAME:-root}
      MONGO_INITDB_ROOT_PASSWORD: ${MONGO_ROOT_PASSWORD:?MongoDB root password must be set}
    volumes:
      - ${DOCKER_VOLUMES}/mongodb/data:/data/db
      - ./config/initdb:/docker-entrypoint-initdb.d
    ports:
      - 27017:27017
    networks:
      - proxy
    depends_on:
      mongodb-init:
        condition: service_completed_successfully
    healthcheck:
      test: ["CMD", "mongosh", "--quiet", "--eval", "db.adminCommand('ping')"]
      interval: 10s
      timeout: 5s
      retries: 5
    labels:
      traefik.enable: false
      homepage.group: Storage
      homepage.name: MongoDB
      homepage.icon: mongodb.png
      homepage.description: "Popular NoSQL document database"

networks:
  proxy:
    external: true