Blackfire Integration with Docker is fully covered in the official documentation: https://blackfire.io/docs/integrations/docker
But here is a short summary on how you add support of Blackfire to your project Docker Compose setup.
Pre-requisites
1. You’ll need to have a Blackfire account
Blackfire stores all profiles on their servers so you need access to them.
You will be given a Server ID, Server Token, Client ID and Client Token, which you will need later for configuration. These could be found here: https://blackfire.io/my/settings/credentials.
2. You’ll (most probably) need a Blackfire Companion in your browser
It connects to your account and helps starting profiling directly from your browser. Install and configure it as described here:
- Chrome: https://blackfire.io/docs/integrations/chrome
- Firefox: https://blackfire.io/docs/integrations/firefox
Integrate Blackfire with your Docker Compose
1. Add Blackfire Agent to your network
Blackfire Agent sends your local profiling results to the Blackfire servers. The easiest way is to add it as a separate container using the official blackfire/blackfire image:
docker-compose.yaml
1 | services: |
The Agent needs Server ID and Server Token to communicate with Blackfire API. You can configure it using environment variables.
docker-compose.override.yaml
1 | services: |
Find out more about docker-compose.override.yaml
file here: Using override file.
2. Add Blackfire PHP Probe and CLI tool to your application container
Blackfire Probe collects the execution stats and sends it to the Agent.
Considering that we base on PHP-FPM image add the following lines to your php Dockerfile:
Dockerfile
1 | FROM php:7.2-fpm |
The CLI tool needs Client ID and Client Token to communicate with Blackfire API. You can configure it using environment variables.
docker-compose.override.yaml
1 | services: |
3. Point your PHP Probe to your Agent
Add the zz-blackfire.ini
file to your php-related configuration files in the repo. In my case it’s: docker/local/php/conf/zz-blackfire.ini
.
Put the following contents to it:
zz-blackfire.ini
1 | ; priority=90 |
Here we set the agent_socket
to tcp://blackfire_agent:8707
, where “blackfire_agent” is the name of your Agent’s service added above and “8707” is the default port on which the Agent is listening.
Then mount this file into your application container:
docker-compose.yaml
1 | services: |
That’s it
Re-build your containers and you are good to go.
Profiling from CLI
Blackfire CLI tool can be used to profile your PHP CLI commands or simple scripts. We installed it to the application container so here is how you run it:
1 | docker-compose exec your_php_app_service blackfire run bin/magento list |
If you need this often I’d suggest to create a bin helper.
Profiling with PHP SDK
With Blackfire PHP SDK you can trigger profiling from PHP code and profile a specific piece of code in your app. You will also have access to profiling results programmatically, which will give you more flexibility to analyze them.
PHP SDK same as CLI tool uses Client ID and Client Token configuration from environment variables.
Add Blackfire PHP SDK as a dev dependency to your project
1 | composer require blackfire/php-sdk --dev |
Or add the latest package version manually to your composer.json
:
composer.json
1 | "require-dev": { |
That’s it.
The simplest way to profile a piece of code is:
1 | $blackfire = new \Blackfire\Client(); |
After putting this just open a page in browser or run the CLI command, no need to run profiling with a Companion.