Using Blackfire with Docker

By Fabien Potencier, on Jan 06, 2015

docker

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:

Happy profiling!

Fabien Potencier

Fabien Potencier is the CEO and founder of Blackfire.io. He founded the Symfony project in 2004 as he constantly looked for better ways to build websites. Fabien is also the creator of several other Open-Source projects, a writer, a blogger, a speaker at international conferences, and the happy father of two wonderful kids.