~/docker/47-how-connect-php-docker-container-mongo-db-docker-container.md

How to connect Php docker container with Mongo DB docker container

2 min readMarch 06, 2018by Gorav Singal#docker#php-mongodb#php-code
// TL;DR

Use Docker's --link flag or a shared Docker network to connect PHP and MongoDB containers. Configure the PHP MongoDB extension and point the connection string to the MongoDB container name.

How to connect Php docker container with Mongo DB docker container

Introduction

In this post, I will show:
  • How to run a mongo dbdocker container?
  • How to run Php with apache docker container?
  • How to connect Php docker container to mongo?
  • How to run some basic mongo dbqueries from phpcontainer?

Run a Mongo DB Docker Container?

Goto Docker for MongoDb. And, select mongo db image you want to run.

Goto your command terminal.

Type:

docker run -d --name my-mongo mongo:latest

This will expose port: 27017 by default. You can connect to this mongo db instance by installing Robo 3T, a software for managing mongo db.

Run a Php docker container

I will run php5/apache container.

Run:

docker run -d -p 8020:80 --name php-apache php:5-apache

Note: This will run a php container, but in order to be able to connect to mongo db container, you need to link this container to mongo db container.

docker run -d -p 8020:80 --link my-mongo --name php-mongo-test php:5-apache

Now, you should be able to see two container running by typing: “docker ps” command.

Install Mongo Php connector

You will need mongo php library in order to write php code that connect to mongo db.

There are two ways:

  1. Run php/apache image, and install php-mongo manually
  2. Write a custom Dockerfile, and prepare your image to have php-mongo dependencies installed.

1. Run php/apache image, and install php-mongo manually

Check container id of php container, by typing command:
docker ps

Open a shell/bash in that container: (assumming 9da60559db80 is my container id)

docker exec -it 9da60559db80 bash

Now, you are into the shell terminal of php container. You will need to install php-mongo dependencies.

Run following commands:

   apt-get update
   apt-get install openssl libssl-dev libcurl4-openssl-dev
   pecl install mongo
   echo "extension=mongo.so" > /usr/local/etc/php/conf.d/mongo.ini

In above steps, we basically installed few dependencies required for mongo db connector, and installed mongo db php extension, and included that in php.ini list.

Note: Php container loads all ini file present in /usr/local/etc/php/conf.d/ directory

Now, you need to restart your container in order to load mongo db extension.

Restart your container:

docker stop 9da60559db80
docker start 9da60559db80

2. Prepare docker image, with above steps baked in

Goto:https://github.com/GyanByte/php-mongo-docker, and build your image. You will have above steps pre-done. Enjoy.

To test whether you have loaded mongo db extension correctly or not. Prepare a phpfile in /var/www/html directory say info.php, and put following content:

<?php
print phpinfo();

On your browser, try: localhost:8082/info.php

You should see a big html page showing php information, and installed extensions. Search for mongo, and it should show some results.

Run Php code that connects to Mongo DB

<?php
$connection = new MongoClient( "mongodb://my-mongo:27017" );

$collection = $connection->selectCollection('db-name', 'collection-name');
if (!$collection) {
        echo 'not connected to collection';
        exit;
}
$cursor = $collection->find();
foreach ($cursor as $doc) {
    var_dump($doc);
}

Bonus

We have pushed this image to hub.docker.com athttps://hub.docker.com/r/gyanbyte/php-apache-mongo/

Php Class Reference for MongoCollection

http://php.net/manual/en/class.mongocollection.php

$ subscribe --weekly

One deep dive a week, in your inbox

System design, AI engineering, trading systems and interview prep. Tested code, real numbers. Free, and you can unsubscribe any time.

or read it on gyanbyte.substack.com

Share

Related Posts

How to install Mongo DB Driver for Php 7.x

How to install Mongo DB Driver for Php 7.x

The simplest way to install driver for php is using pecl. When I tried to run…

Docker image for Drupal 7, and Php extension MongoDB installed.

Docker image for Drupal 7, and Php extension MongoDB installed.

You have drupal 7 image from docker hub, and want to connect tomongo db via php…

How to take Backup from MongoDB and Restore to MongoDB

How to take Backup from MongoDB and Restore to MongoDB

This will take backup of your passed database name, to the passed folder. It…

Drupal Helpful codes for database queries

Drupal Helpful codes for database queries

Being a drupal user from last around 5 years, I used to know small codes for…

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Docker Push&#58; How to push your docker image to your organization in hub.docker.com

Tag the image, by seeing its image id, from docker images command docker tag 04d…

How to get all image tags from an html - php code

How to get all image tags from an html - php code

I wanted to fetch all image tags from a big html, and wanted to perform some…

Latest Posts

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

How to Stay Valuable as a Software Engineer in the AI Era: A Practical Playbook

In the previous article I argued that AI is not killing software engineering…

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

How to Review AI-Generated Code: A Checklist for Catching What the Model Missed

A few months back I approved a PR that was almost entirely AI-generated. It…

How to Debug AI-Generated Code: Understand First, Fix Last

How to Debug AI-Generated Code: Understand First, Fix Last

In the last article I wrote about reviewing AI code before it goes in. This one…

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

The Future of Software Engineering: What AI Changes, What Stays, and Where the Jobs Go

Every few weeks somebody sends me a scary headline — “AI will replace all…

The Transactional Outbox Pattern: Never Lose an Event Again

The Transactional Outbox Pattern: Never Lose an Event Again

This is one of those patterns that looks like nothing until you get burnt once…

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Event Sourcing, Explained: Store What Happened, Not Just the Final State

Here is a question I like to ask in interviews: your bank account shows a…