MongoDB
What is MongoDB?
MongoDB is a document-oriented NoSQL database that stores data as flexible, JSON-like documents instead of rows and tables. It's a common choice for apps with unstructured or rapidly evolving schemas. Laradock runs it as its own container.
Start MongoDB
docker compose up -d mongo
Stop MongoDB
docker compose stop mongo
This stops the container without deleting its data. To remove the container (data on disk is untouched, it lives under DATA_PATH_HOST): docker compose rm -f mongo.
Configuration
All settings live in mongo/defaults.env and can be overridden by adding the same line to your own .env:
| Variable | Default | What it does |
|---|---|---|
MONGODB_PORT | 27017 | Host-side port MongoDB is published on (host:container). |
MONGO_USERNAME | root | Root user created automatically on first boot (MONGO_INITDB_ROOT_USERNAME). |
MONGO_PASSWORD | example | Password for the root user (MONGO_INITDB_ROOT_PASSWORD). |
Data persists under DATA_PATH_HOST/mongo (database files) and DATA_PATH_HOST/mongo_config (config db).
Use MongoDB from Laravel
- In
.env, set both flags totrue:WORKSPACE_INSTALL_MONGOPHP_FPM_INSTALL_MONGO
- Rebuild the containers that need the PHP MongoDB driver:
docker compose build workspace php-fpm
- Start the database container:
docker compose up -d mongo
- Add a MongoDB connection to
config/database.php:'connections' => ['mongodb' => ['driver' => 'mongodb','host' => env('DB_HOST', 'localhost'),'port' => env('DB_PORT', 27017),'database' => env('DB_DATABASE', 'database'),'username' => '','password' => '','options' => ['database' => '',]],// ...], - In your Laravel
.env, setDB_HOST=mongo,DB_PORT=27017, andDB_DATABASE=database. - Install the Laravel MongoDB package (formerly
jenssegers/mongodb, now maintained asmongodb/laravel-mongodb):composer require mongodb/laravel-mongodb - Extend your models from the MongoDB Eloquent model, enter the Workspace, and run
php artisan migrate.
Connect from your host machine
Inside Laradock, other containers reach MongoDB by container name: DB_HOST=mongo. From your own machine, connect a GUI client (Compass, Studio 3T) to localhost on MONGODB_PORT (27017 by default) using MONGO_USERNAME/MONGO_PASSWORD.
Want a browser-based admin UI instead of a desktop client? See Mongo WebUI.
Common issues
- Auth fails right after first boot.
MONGO_USERNAME/MONGO_PASSWORDare only applied when the data folder is created for the first time. Change them afterward and either dropDATA_PATH_HOST/mongo(data loss) or create the new user manually via the Mongo shell. - App can't connect but the container is running. Confirm the app's
.envusesDB_HOST=mongo(the container name), notlocalhost, which only works from your host machine. - Driver not found in PHP. The Mongo PHP extension isn't installed by default, you must set
WORKSPACE_INSTALL_MONGOandPHP_FPM_INSTALL_MONGOtotrueand rebuild beforecomposer require mongodb/laravel-mongodbwill work. - Port already in use on your host. Another local MongoDB (or another Laradock project) is already bound to
27017. ChangeMONGODB_PORTin.envand restart.
Need a graph database instead? See Neo4j. For the full list of services, see Getting Started.