Railway

Deploy MongoDB

Run MongoDB 7 on Railway with official image and default port.

Deploy MongoDB

Just deployed

MongoDB for railway.app

Template Header

Deploy MongoDB 7 on Railway with the official Docker image.

Deploy on Railway

πŸ—οΈ Architecture

flowchart LR
    Client(["πŸ“¦ App / Client"]) -->|"MongoDB Wire Protocol"| Proxy["Railway TCP Proxy"]
    Proxy -->|"$PORT β†’ 27017"| App["Container\nmongo:7"]
    App --> Volume[("Volume\n/data/db")]

Environment

VariableRequiredDescription
MONGO_INITDB_ROOT_USERNAMEYesRoot username created on first start of an empty volume. Set this in the Railway dashboard as a generated secret.
MONGO_INITDB_ROOT_PASSWORDYesRoot password created on first start of an empty volume. Set this in the Railway dashboard as a generated secret.

Both variables must be set together before the first deployment β€” MongoDB only enables authentication and creates the root user on an empty data directory. Deploying without them leaves the database unauthenticated and reachable by anyone with network access to it. See .env.example for local development.

Optional

VariableDescription
MONGO_INIT_DUMP_URLHTTPS URL to a mongorestore-compatible single-file archive (mongodump --archive[--gzip] output, not a mongodump --out directory tree); downloaded and restored via mongorestore --archive --drop automatically, but only on the first start of an empty volume. Takes precedence over MONGO_INIT_DUMP_BASE64 if both are set.
MONGO_INIT_DUMP_BASE64Same archive, base64-encoded and pasted directly as the variable value. Ignored if MONGO_INIT_DUMP_URL is also set. Best for small dumps only.

> [!NOTE] > MONGO_INIT_DUMP_URL/MONGO_INIT_DUMP_BASE64 are only ever consulted on the very first start against a brand-new, empty volume. Once the database has initialized, both variables are silently ignored on every later restart or redeploy β€” even if you leave them set. The restore runs with full administrative privileges during bootstrap, so only point these at dumps from sources you trust.

Health Check

The image defines a HEALTHCHECK that runs mongosh --eval "db.adminCommand('ping')" every 30 seconds so container orchestrators (including Railway) can detect an unhealthy instance.

Persistence

railway.toml declares requiredMountPath = "/data/db". Attach a Railway volume to that path before production traffic β€” Railway will prompt for it based on this setting, but it is not created automatically.

Local

cp .env.example .env
docker build -t railwayapp-mongodb .
docker run --rm -p 27017:27017 --env-file .env railwayapp-mongodb

Airbyte Apache Airflow CloudBeaver CodiMD Django Email Service FastAPI Flask Flowise GitLab CE Grafana Home Assistant InfluxDB MJML MongoDB Mosquitto MQTT MySQL n8n Node.js Node-RED OpenSearch Open WebUI Outerbase Studio PostgreSQL Redis TYPO3 CMS


Template Content

More templates in this category

View Template
Rocky Linux
[Jul'26] Hosted Rocky Linux 9 workspace with SSH and persistent storage. πŸš€

codestorm
40
View Template
Foundry Virtual Tabletop
A Self-Hosted & Modern Roleplaying Platform

Lucas
71
View Template
Letta Code Remote
Run a Letta Code agent 24/7. No inbound ports, just deploy.

Letta
51