Run MongoDB

Last updated

Same pattern as Postgres; read Databases first for the context that matters — the port will be publicly reachable.

Create the database app

Use Existing Image:

  • Image: mongo:7 — pin a version rather than using latest
  • Volume: /data/db
  • Port: container 27017, protocol TCP
  • Environment variables:
    • MONGO_INITDB_ROOT_USERNAME
    • MONGO_INITDB_ROOT_PASSWORD

Set both. They're marked optional in the image's documentation, and without them the instance starts with no authentication at all — which is fine on a laptop and not fine on a public endpoint. This is the single most important step on this page.

Note that these only take effect on first start, when the data directory is empty. Setting them later on an existing volume does nothing.

Connect your application

A TCP binding is published on a generated high port, not 27017. Take the host and port from the Ports tab:

MONGODB_URI=mongodb://USER:PASSWORD@HOST:PORT/DBNAME?authSource=admin

Create the database app first, read its address, then set the variable on your application.

Because it's public

  • Long random credentials, and an application user rather than root for day-to-day use.
  • Remove the port binding when nothing external needs it.
  • Enable TLS if your driver supports it.
  • Keep the connection pool small — driver defaults are sized for larger machines than a fractional-CPU container.

Sizing

MongoDB is more memory-hungry at rest than Postgres or SQLite, and it will make use of whatever it's given. On a small allocation, watch for out-of-memory kills (exit code 137 in the app's events) and raise the preset if you see them.

If your data is small and only one process touches it, SQLite on a volume is a lighter answer.

Backups

mongodump --uri "mongodb://USER:PASSWORD@HOST:PORT" --out /data/backup

Write to a volume, download from the Files tab or ship off-platform, and prune old dumps — volume size counts toward the account disk limit that stops every app when exceeded.