August 12, 2017

Getting xDebug to Work

See how to configure PHPStorm and Xdebug (in a container) to communicate with eachother.

PHP Storm

We start by configuring PHPStorm. Configuration is in two parts:

  1. Configuring a Server
  2. Configuring a new PHP Remote Debugger

Those settings look like this:

xdebug phpstorm

Docker

We need a few things to start:

  1. A Dockerfile to build an image with Xdebig
  2. An xdebug.ini configuration
  3. A script to use as a CMD (to start off a process)

 

File Dockerfile

FROM ubuntu:16.04

MAINTAINER Chris Fidao

RUN locale-gen en_US.UTF-8

ENV LANG en_US.UTF-8
ENV LANGUAGE en_US:en
ENV LC_ALL en_US.UTF-8

RUN apt-get update \
    && apt-get install -y curl zip unzip git software-properties-common sqlite3 \
    && add-apt-repository -y ppa:ondrej/php \
    && apt-get update \
    && apt-get install -y php7.0-fpm php7.0-cli php7.0-mcrypt php7.0-gd php7.0-mysql \
       php7.0-pgsql php7.0-imap php-memcached php7.0-mbstring php7.0-xml php7.0-curl \
       php7.0-sqlite3 php7.0-xdebug \
    && php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
    && mkdir /run/php \
    && apt-get remove -y --purge software-properties-common \
    && apt-get -y autoremove \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

COPY xdebug.ini /etc/php/7.0/mods-available/xdebug.ini

COPY start-container /usr/local/bin/start-container
RUN chmod +x usr/local/bin/start-container

EXPOSE 80
CMD ["start-container"]

File xdebug.ini

zend_extension=xdebug.so
xdebug.remote_enable=1
xdebug.remote_handler=dbgp
xdebug.remote_port=9000
xdebug.remote_autostart=1
xdebug.remote_connect_back=0
xdebug.idekey=docker
xdebug.remote_host=???

We can use my Macintosh's IP address. We use my Mac's private network IP address for the remote_host, as this configuration is telling xdebug where to reach PHPStorm, which will be listening on my host machine for xdebug connections.

This means the container is reaching out of the container to my host machine to send debugging information.

ipconfig getifaddr en1
# 192.168.1.2

So, we'll see xdebug.remote_host=192.168.1.2 in xdebug.ini for now.

File start-container

We use a bash script named start-container as the default command for the image. This is a simple bash script:

#!/usr/bin/env bash

php -S 0.0.0.0:80 -t /var/www/html

File docker-compose.yml

Let's use a docker-compose configuration to make this a little easier on ourselves:

version: '2'
services:
  app:
    build:
      context: .
    image: xdebug-example:latest
    volumes:
     - ./app:/var/www/html
    ports:
     - "80:80"

An "application"

We'll make a super small "application":

mkdir app
vim app/index.php

File index.php:

<?php

$one = 1;
$two = 2;

$three = $one + $two;

echo ($three === 3) ? '1 plus 2 equals 3!' : 'Something is terribly wrong';

Build the image and run it

docker-compose build
docker-compose up -d

Then once we set a breakpoint in PHPStorm and start debugging, we'll see that xdebug sends debugging information to PHPStorm!

Looking for a deeper dive into Docker?

Sign up here to get a preview of the Shipping Docker course! Learn how to integrate Docker into your applications and develop a workflow to make using Docker a breeze!

All Topics