Docker Hub: https://hub.docker.com/r/edyan/xhgui
Docker containers that runs xhgui (which needs mongodb and PHP).
It's based on :
- edyan/php:5.6 image (jessie stable).
- or edyan/php:7.2 image (stretch stable).
It's made for development purposes. You need to find the right version for your project.
Use 5.6 for PHP 5.6 projects and 7.2 for PHP 7.x projects. Just make sure you have the
mongodb
extension enabled (v1.5) on your main PHP container.
To use it in an integrated environment, try Stakkr
To make it work, you need to link it to an existing PHP environment. Example via docker-compose.yml
:
version: '2'
services:
xhgui:
image: edyan/xhgui:php7.2
# I need to access xhgui
ports:
- "9000:80"
volumes:
- ./xhgui-config.php:/usr/local/src/xhgui/config/config.php
php:
hostname: php
command: /usr/bin/php -S 0.0.0.0:80 -t /var/www
image: edyan/php:7.2 # That image contains mongodb extension from PECL
# To have the new mounted volumes as well as the default volumes of xhgui (its source code)
volumes_from: [xhgui]
ports:
- "8000:80"
volumes:
- ./src:/var/www
As seen above, you need to mount your own configuration file that connects to the right mongodb server. The xhgui-config.php
file, in our case (see the official xhgui repo), will contain (note the db.host):
<?php
return array(
'debug' => false,
'mode' => 'development',
'save.handler' => 'mongodb',
'db.host' => 'mongodb://xhgui',
'db.db' => 'xhprof',
'db.options' => array(),
'templates.path' => dirname(__DIR__) . '/src/templates',
'date.format' => 'M jS H:i:s',
'detail.count' => 6,
'page.limit' => 25,
'profiler.enable' => function () {
return true;
},
'profiler.simple_url' => function ($url) {
return preg_replace('/\=\d+/', '', $url);
}
);
And the src/index.php
:
<?php
require_once('/usr/local/src/xhgui/external/header.php');
function test_xhgui()
{
$data = [];
for ($i = 0; $i < 5000; $i++) {
$data[] = $i * $i;
sort($data);
}
}
test_xhgui();
Finally, launch the environment with : docker-compose up --force-recreate
.
Then call http://localhost:8000/index.php in your browser and get reports
from http://localhost:9000.
You have two ways to call the profiler. The first one, the most easiest, is to require the file from your script.
For that you must mount the volumes (with volumes_from
) of the xhgui container to your php container. The code is the following:
<?php
require_once('/usr/local/src/xhgui/external/header.php');
// ... Code below
If you want to profile everything then you must override the second one, by altering the php.ini configuration and use PHP's auto_prepend_file directive:
auto_prepend_file=/usr/local/src/xhgui/external/header.php
If you use edyan/php you can override the configuration. See the documentation
XHGUI_MONGO_HOST
default tomongodb://127.0.0.1
, used in XHGui config file.MONGO_PORT
default to 27017PHP_WEBSERVER_PORT
default to 80
A docker-compose
file is available to do some tests:
$ docker-compose -f docker-compose.sample.yml up --force-recreate -d
# Enter the php container
$ docker-compose -f docker-compose.sample.yml exec php bash
# Create a file
$ mkdir -p /var/www
# As we didn't mount the configuration file, we force the env variable for the server
$ echo '<?php putenv("XHGUI_MONGO_HOST=mongodb://xhgui"); require_once("/usr/local/src/xhgui/external/header.php"); $a=[]; for($i=0; $i<10000; $i++){ $a[]=$i; } sort($a); echo "Done";' > /var/www/index.php
Now open http://localhost:8000/index.php to read the new file created. Then http://localhost:9000 to see the report.
Clean :
$ docker-compose -f docker-compose.sample.yml down