# How to run MongoDB in a cloud sandbox Add MongoDB's apt repository in an Ubuntu 24.04 microVM, install mongodb-org, start mongod with spawn, and load data with mongoimport. **On Runtime, each test or agent gets a MongoDB on a machine of its own.** A new sandbox ran its first command 351 ms after the create request at the median on 24 September 2026 ([speed](/docs/speed)); install MongoDB once in an image and every sandbox after that has it on disk, ready for `mongod`. Each one is a Firecracker microVM with its own kernel, $0.03125 an hour for 2 vCPU and 4 GiB while it waits on queries ([pricing](/docs/pricing)). ## Where MongoDB comes from MongoDB is not in Ubuntu's archive: there is no `mongodb` package for 24.04. MongoDB's install guide, read 25 September 2026, covers MongoDB 8.3 Community Edition and lists Ubuntu 24.04 "Noble" as supported on x86_64, which is what a sandbox runs. | Package | What it holds | | -------------------- | -------------------------------------------------- | | `mongodb-org` | The meta-package: server, database tools and shell | | `mongodb-org-server` | `mongod`, about 160 MB installed | | `mongodb-org-tools` | `mongoimport`, `mongoexport`, `mongodump` and more | | `mongodb-mongosh` | `mongosh`, the shell | The installed size is the one MongoDB's noble repository listed for `mongodb-org-server` on 25 September 2026. The official Docker image, `mongo:8.3`, is the other route, with [Docker in a sandbox](/how-to/run-docker-in-a-sandbox). ## Install, start, load, query ```ts check import { writeFile } from "node:fs/promises"; import { Sandbox } from "withruntime"; const repo = [ "curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-8.3.list', "sudo apt-get update -qq", "sudo apt-get install -y -qq mongodb-org", ].join(" && "); await using sbx = await Sandbox.create({ diskMiB: 6144, timeoutSeconds: 1800 }); await sbx.exec(repo, { check: true, timeoutMs: 600_000 }); await sbx.exec("mkdir -p /workspace/db", { check: true }); await sbx.spawn("mongod --dbpath /workspace/db --bind_ip 127.0.0.1 --port 27017"); await sbx.exec( "timeout 60 bash -c 'until mongosh --quiet --eval \"db.runCommand({ ping: 1 }).ok\"; do sleep 1; done'", { check: true, timeoutMs: 90_000 }, ); await sbx.files.upload("./products.json", "/workspace/products.json"); await sbx.exec("mongoimport --db shop --collection products --jsonArray --file products.json", { check: true, timeoutMs: 300_000, }); const cheap = await sbx.exec( `mongosh shop --quiet --eval 'db.products.countDocuments({ price: { $lt: 20 } })'`, { check: true }, ); console.log("under $20:", cheap.stdout.trim()); await sbx.exec("mongoexport --db shop --collection products --out /workspace/out.json", { check: true, }); await writeFile("out.json", await sbx.files.read("/workspace/out.json")); ``` ```python check from withruntime import Sandbox REPO = " && ".join([ "curl -fsSL https://pgp.mongodb.com/server-8.0.asc | sudo gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] ' 'https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3 multiverse" ' "| sudo tee /etc/apt/sources.list.d/mongodb-org-8.3.list", "sudo apt-get update -qq", "sudo apt-get install -y -qq mongodb-org", ]) with Sandbox.create(disk_mib=6144, timeout_seconds=1800) as sbx: sbx.exec(REPO, check=True, timeout_ms=600_000) sbx.exec("mkdir -p /workspace/db", check=True) sbx.spawn("mongod --dbpath /workspace/db --bind_ip 127.0.0.1 --port 27017") sbx.exec("timeout 60 bash -c 'until mongosh --quiet --eval \"db.runCommand({ ping: 1 }).ok\"; " "do sleep 1; done'", check=True, timeout_ms=90_000) sbx.files.upload("products.json", "/workspace/products.json") sbx.exec("mongoimport --db shop --collection products --jsonArray --file products.json", check=True, timeout_ms=300_000) print(sbx.exec("mongosh shop --quiet --eval 'db.products.countDocuments({ price: { $lt: 20 } })'", check=True).stdout) sbx.exec("mongoexport --db shop --collection products --out /workspace/out.json", check=True) sbx.files.download("/workspace/out.json", "out.json") ``` - The key and repository lines are MongoDB's own for noble, including its key file name, `server-8.0.asc`, for the 8.3 repository. - `mongod` runs as the sandbox user with its data in `/workspace/db`, so it needs no `sudo`, and `spawn` keeps it running after the call returns. - The repository is served over HTTPS on port 443, which the free trial reaches. ## Hand an agent a database it can wreck A model that writes queries or migrations gets things wrong, sometimes destructively. Run its code in the same sandbox, with the internet off once MongoDB is installed, and a bad `deleteMany({})` costs one sandbox: ```ts check import { Sandbox } from "withruntime"; declare const db: Sandbox; // the sandbox above, data loaded declare const agentScript: string; // the query the model wrote await db.network.off(); // the model's code can reach nothing outside await db.files.write("/workspace/agent.js", agentScript); const result = await db.exec("mongosh shop --quiet --file agent.js", { timeoutMs: 120_000 }); console.log(result.exitCode, result.stdout.slice(0, 2000)); ``` Network rules are enforced on the host, so root in the sandbox cannot turn them back on. To give each attempt a fresh copy of the loaded data, fork the sandbox before running it ([sandbox fork](/glossary/sandbox-fork)). ## Open it in Compass ```bash no-run runtime sandbox port-forward 27017 ``` Connect Compass, or your app, to `mongodb://127.0.0.1:27017/?directConnection=true`. The forward runs through Runtime's API with your key, on the free trial too, and the sandbox opens no port to the internet ([forward ports](/docs/editors#forward-ports)). From code, `sbx.forwardPort(27017)` or `sbx.forward_port(27017)` does the same. ## Start every sandbox with MongoDB installed Recipe commands run as root, so the repository lines drop their `sudo`: ```ts check import { Runtime } from "withruntime"; const runtime = new Runtime(); await runtime.images.build({ name: "mongodb", recipe: { commands: [ "curl -fsSL https://pgp.mongodb.com/server-8.0.asc | gpg -o /usr/share/keyrings/mongodb-server-8.0.gpg --dearmor", 'echo "deb [ arch=amd64,arm64 signed-by=/usr/share/keyrings/mongodb-server-8.0.gpg ] https://repo.mongodb.org/apt/ubuntu noble/mongodb-org/8.3 multiverse" > /etc/apt/sources.list.d/mongodb-org-8.3.list', "apt-get update && apt-get install -y mongodb-org", ], }, }); await using sbx = await runtime.sandboxes.create({ image: "mongodb" }); ``` Building is free and uses no trial hours; the free trial stores three images free ([custom images](/docs/images)). ## Sizing - **Disk.** The server alone is about 160 MB installed, plus the tools, the shell and your data. The default 4 GiB disk had about 2.5 GiB free on 24 September 2026; the samples ask for 6 GiB, and the trial allows 10. - **Memory.** MongoDB's cache lives in the sandbox's memory, which is what you ask for with `memoryMiB` and is billed at $0.0075 a GiB-hour while running. - **Keeping data.** A pause keeps the running `mongod` and its memory. For a disk that outlives sandboxes, put `--dbpath` on a [volume](/docs/javascript#volumes). For relational data, see [PostgreSQL in a sandbox](/integrations/postgres) and [MySQL in a sandbox](/integrations/mysql). ## Sources - MongoDB, install Community Edition on Linux, https://www.mongodb.com/docs/manual/administration/install-community-linux/, read 25 September 2026 - MongoDB noble repository, https://repo.mongodb.org/apt/ubuntu/dists/noble/mongodb-org/8.3/, read 25 September 2026 - Ubuntu package search for noble, https://packages.ubuntu.com/noble/, read 25 September 2026 - Docker Hub tags for mongo, https://hub.docker.com/_/mongo, read 25 September 2026 Facts on this page were checked on 25 September 2026.