This simple recipe creates two new containers, one for MongoDB and one for Mongo Express.
Based on MongoDb from Docker Hub, ddev custom compose files and API Platform tutorial.
I'm using it on a Symfony 4 app with API Platform.
Steps to follow:
-
Install php-mongo extension by adding
webimage_extra_packages: [php7.3-mongodb]
to your .ddev/config.yaml. Note that the PHP version in the package name must correspond to the PHP version you are running, e.g. usephp7.4-mongodb
if your server runs PHP 7.4. -
Add the file docker-compose.mongo.yaml to the project's
.ddev
directory. -
Require the Doctrine MongoDB ODM bundle
ddev composer require doctrine/mongodb-odm-bundle:^4.0.0@beta doctrine/mongodb-odm:^2.0.0@beta
-
In your application
.env
, set the connection string:MONGODB_URL=mongodb://db:db@ddev-<project>-mongo:27017 MONGODB_DB=api
Mongo Express will now be accessible from http://<project>.ddev.site:8081
Caveats:
- You can't define custom MongoDB configuration with this current setup.
- You can't use
ddev import-db
to import to mongo.
Contributed by @wtfred