Docker Php Mongodb Extension

  



Image with the ID e3e8c8dc05e5 is a barebones Alpine image with just PHP installed, give or take 3–5 PHP extensions (28.7MB) So there’s no reason for the PHP image to be 127MB. The thing is, I’m building this custom Docker image for running Drupal. Adminer MongoDB docker image. GitHub Gist: instantly share code, notes, and snippets.

Example

Requirements

  • MongoDB server running on port usually 27017. (type mongod on command prompt to run mongodb server)

  • Php installed as either cgi or fpm with MongoDB extension installed(MongoDB extension is not bundled with default php)

  • Composer library(mongodb/mongodb).(In the project root run php composer.phar require 'mongodb/mongodb=^1.0.0' to install the MongoDB library)

If everything is ok you are ready to move on.

Check For Php installation

if not sure check Php installation by running php -v on command prompt will return something like this

PHP 7.0.6 (cli) (built: Apr 28 2016 14:12:14) ( ZTS ) Copyright (c) 1997-2016 The PHP Group Zend Engine v3.0.0, Copyright (c) 1998-2016 Zend Technologies

MongodbPhp

Check For MongoDB installation

Check MongoDB installation by running mongo --version will return MongoDB shell version: 3.2.6

Check For Composer installation

Check for Composer installation by running php composer.phar --version will return Composer version 1.2-dev (3d09c17b489cd29a0c0b3b11e731987e7097797d) 2016-08-30 16:12:39 `

Connecting to MongoDB from php

The above code will connect using MongoDB composer library(mongodb/mongodb) included as vendor/autoload.php to connect to the MongoDB server running on port: 27017. If everything is ok it will connect and list an array, if exception occurs connecting to MongoDB server the message will be printed.

CREATE(Inserting) into MongoDB

Mongodb

In the example we are using the $mongo instance previously used in the Connecting to MongoDB from php part. MongoDB uses JSON type data format, so in php we will use array to insert data into MongoDB, this conversion from array to Json and vice versa will be done by mongo library. Every document in MongoDB has a unique id named as _id,during insertion we can get this by using $result->getInsertedId();

READ(Find) in MongoDB

Drop in MongoDB

There are many methods that can be performed on $collection see Official documentation from MongoDB


Docker Php Mongodb Extension Chrome