Using Blackfire with Docker
Starting today, Blackfire fully supports Docker to ease the profiling of your web applications using this platform.
Docker is everywhere! And today, I’m very excited to announce that Blackfire now fully supports Docker as a platform.
Docker is a great way to isolate processes and being able to isolate the Blackfire Agent or the Blackfire Client in their own containers is now easily possible via the officialblackfire/blackfire
image.
The first example I want to show you is how you can use the Blackfire Docker image to easily profile remote web servers… without having to install anything on your machine (the Docker image size is less than 18Mo.)
Run a blackfire/blackfire
container and pass the curl
command you want to execute:
docker run -it --rm \ -e BLACKFIRE_CLIENT_ID=$BLACKFIRE_CLIENT_ID \ -e BLACKFIRE_CLIENT_TOKEN=$BLACKFIRE_CLIENT_TOKEN \ blackfire/blackfire blackfire \ --slot=7 --samples=10 \ curl http://symfony.com/
Create an alias to make it less verbose:
alias blackfire-curl='docker run -it --rm \ -e BLACKFIRE_CLIENT_ID=$BLACKFIRE_CLIENT_ID \ -e BLACKFIRE_CLIENT_TOKEN=$BLACKFIRE_CLIENT_TOKEN \ blackfire/blackfire blackfire'
And use the alias like this:
blackfire-curl --slot=7 --samples=10 curl http://symfony.com/
The Docker Blackfire image also comes with the Blackfire Agent daemon:
docker run -d -e BLACKFIRE_SERVER_ID=$BLACKFIRE_SERVER_ID -e BLACKFIRE_SERVER_TOKEN=$BLACKFIRE_SERVER_TOKEN blackfire/blackfire
As the image exposes the Agent socket, linking it to your PHP container is very easy. Here is a simple PHP Dockerfile
that illustrates how to add the Blackfire PHP probe:
FROM php:5.6-apache RUN curl -A "Docker" -o /tmp/blackfire-probe.tar.gz -D - -L -s https://blackfire.io/api/v1/releases/probe/php/linux/amd64/56 \ && tar zxpf /tmp/blackfire-probe.tar.gz -C /tmp \ && mv /tmp/blackfire-*.so `php -r "echo ini_get('extension_dir');"`/blackfire.so \ && echo "extension=blackfire.so\nblackfire.agent_socket=\${BLACKFIRE_PORT}" > $PHP_INI_DIR/conf.d/blackfire.ini
The ${BLACKFIRE_PORT}
variable is the magic that makes it easy to link the PHP container to the agent one:
docker run -d -p 8080:80 --link blackfire:blackfire -v `pwd`:/var/www/html php-blackfire
where php-blackfire
is the name of the image you built with the above Dockerfile configuration.
If you are a Docker user, common use cases are explained with detailed examples in our updated documentation:
- Blackfire Installation and Configuration for Docker;
- Profiling Web Services and API Calls with Docker.
- Docker integration
Happy profiling!