Profiling Laravel Artisan Commands Inside a Docker Container

Laravel Herd Pro has a really slick SPX profiler integration. Unfortunately, at work I’m on a Ubuntu machine using a custom Docker container to run our app, so can’t take advantage of it.

We have one specific artisan command that takes many hours to run and I’m working on improving the performance.

I wanted to use SPX profiler to track down what was taking so long, and here’s how I got it to work.

Note: I know this is a bit of an unorthodox approach to using Docker containers, but it works for now…

Step-By-Step

  1. Run docker compose exec www bash to open a terminal inside the container
  2. Install SPX following the directions (note: skip the sudo since we’re already running as a root user inside the container)
  3. Edit /usr/local/etc/php/php.ini and add these lines to enable SPX
    extension = spx.so
    spx.http_enabled = 1
    spx.http_key = "dev"
    spx.http_ip_whitelist = "*"
    spx.http_trusted_proxies = "*"
  4. Run pkill -o -USR2 php-fpm inside the container to restart the PHP service (thanks for the tip)
  5. In a browser, visit http://myapp.test/?SPX_KEY=dev&SPX_UI_URI=/ to visit the SPX Control Panel
  6. Check the “Enabled” box
  7. Open a new tab and visit page or two on the app; refresh the SPX Control Panel and you should see the requests show up at the bottom
  8. Run the artisan command, prefixing it with a couple of env settings: SPX_ENABLED=1 SPX_REPORT=full php artisan list
  9. Refresh the SPX Control Panel and inspect the results

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.