MongoDB
Curated name mongodb. Adds a MongoDB 8 service to the workbench in compose
mode, with seeded dev credentials and a readiness healthcheck.
What it’s for
Section titled “What it’s for”A document database for local development - a real MongoDB your app runs against, with a root user seeded for you. Use it when your stack stores documents/JSON rather than relational rows.
Add it
Section titled “Add it”At init, or to an existing workbench:
monoceros init acme --with-services=mongodbmonoceros add-service acme mongodbNeed a second database? Add it again under a different name with --as - each
instance gets its own host, data dir, and connection env:
monoceros add-service acme mongodb --as=eventsRemove it
Section titled “Remove it”monoceros remove-service acme mongodbOr delete the service’s block from acme.yml by hand and re-run
monoceros apply acme - the yml is the source of truth. (Its data volume monoceros-acme-data-mongodb is removed with the workbench on
monoceros remove, which backs it up first.)
The yml entry
Section titled “The yml entry”services: - name: mongodb # host name in the container + data dir name image: mongo:8 port: 27017 # in-container port (feeds `monoceros tunnel`) env: MONGO_INITDB_ROOT_USERNAME: ${MONGO_INITDB_ROOT_USERNAME} # values live in acme.env MONGO_INITDB_ROOT_PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} MONGO_INITDB_DATABASE: ${MONGO_INITDB_DATABASE} volumes: - data:/data/db # the service's own Docker volume restart: unless-stopped healthcheck: # workspace waits for service_healthy test: [CMD, mongosh, --quiet, --eval, 'db.adminCommand({ ping: 1 }).ok'] interval: 10s timeout: 5s retries: 5 connectionEnv: # → the workspace env vars below URL: mongodb://${MONGO_INITDB_ROOT_USERNAME}:${MONGO_INITDB_ROOT_PASSWORD}@${host}:${port}/${MONGO_INITDB_DATABASE}?authSource=admin HOST: ${host} PORT: ${port} USER: ${MONGO_INITDB_ROOT_USERNAME} PASSWORD: ${MONGO_INITDB_ROOT_PASSWORD} DB: ${MONGO_INITDB_DATABASE}The credentials are ${VAR} references; the real values live in acme.env (dev
default monoceros for user, password, and database), so the yml stays
shareable without baking credentials in (see
Workbench configuration).
What your code sees
Section titled “What your code sees”Monoceros injects a connection env into the workspace container, prefixed with the service name:
| Variable | Value (defaults) |
|---|---|
MONGODB_URL | mongodb://monoceros:monoceros@mongodb:27017/monoceros?authSource=admin |
MONGODB_HOST | mongodb |
MONGODB_PORT | 27017 |
MONGODB_USER | monoceros |
MONGODB_PASSWORD | monoceros |
MONGODB_DB | monoceros |
The URL carries ?authSource=admin because the seeded user is the root user
(authenticated against the admin database). The prefix is the service
name, uppercased, so a second instance added with --as=events gets
EVENTS_URL, … and never collides.