{"id":94,"date":"2017-03-13T18:28:58","date_gmt":"2017-03-13T18:28:58","guid":{"rendered":"http:\/\/magebrew.com\/blog\/?p=94"},"modified":"2017-03-14T07:03:00","modified_gmt":"2017-03-14T07:03:00","slug":"debugging-php-app-using-xdebug-and-remote-docker-php-container","status":"publish","type":"post","link":"https:\/\/magebrew.com\/blog\/debugging-php-app-using-xdebug-and-remote-docker-php-container\/","title":{"rendered":"Debugging PHP app using Xdebug and Remote Docker PHP Container"},"content":{"rendered":"<p>No so long ago I&#8217;ve started using Docker as my primary\u00a0development environment. Before that I had\u00a0everything (WEB-server, PHP, MySql, etc) installed\u00a0on my Mac and being run natively. Docker brings more flexibility and it is just fun. So now my setup includes Mac OS with PhpStorm installed and\u00a0remote Ubuntu server running Docker containers needed for my WEB application.<\/p>\n<p><img loading=\"lazy\" class=\"aligncenter wp-image-97 size-large\" src=\"http:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/docker_xdebug-1024x480.png\" alt=\"Docker Development Environment\" width=\"660\" height=\"309\" srcset=\"https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/docker_xdebug-1024x480.png 1024w, https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/docker_xdebug-300x141.png 300w\" sizes=\"(max-width: 660px) 100vw, 660px\" \/><\/p>\n<p><!--more-->Official PHP5.6-FPM is missing some extensions, so we extend it and build our own. Dockerfile for PHP service:<\/p>\n<pre class=\"lang:default decode:true\" title=\"Dockerfile for PHP\">FROM php:5.6-fpm\r\n\r\nRUN apt-get -qq update &amp;&amp; apt-get -qq install libxml++2.6-dev &gt; \/dev\/null\r\nRUN apt-get update &amp;&amp; apt-get install -y \\\r\n        libfreetype6-dev \\\r\n        libcurl4-gnutls-dev \\\r\n        build-essential \\\r\n        libtool \\\r\n        uuid-dev \\\r\n        libsodium-dev \\\r\n        libjpeg62-turbo-dev \\\r\n        libpng12-dev \\\r\n    &amp;&amp; docker-php-ext-configure gd --with-freetype-dir=\/usr\/include\/ --with-jpeg-dir=\/usr\/include\/ \\\r\n    &amp;&amp; docker-php-ext-install -j$(nproc) gd mbstring mysql mysqli pdo pdo_mysql soap\r\n\r\nRUN apt-get install -y libmcrypt-dev\r\nRUN docker-php-ext-install mcrypt\r\n\r\nRUN apt-get install -y libicu-dev\r\nRUN pecl install intl\r\nRUN docker-php-ext-install intl\r\nRUN yes | pecl install xdebug \\\r\n    &amp;&amp; echo \"zend_extension=$(find \/usr\/local\/lib\/php\/extensions\/ -name xdebug.so)\" &gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini \\\r\n    &amp;&amp; echo \"xdebug.remote_enable=on\" &gt;&gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini \\\r\n    &amp;&amp; echo \"xdebug.remote_port=9001\" &gt;&gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini \\\r\n    &amp;&amp; echo \"xdebug.remote_connect_back=0\" &gt;&gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini \\\r\n    &amp;&amp; echo \"xdebug.idekey=PHPSTORM\" &gt;&gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini \\\r\n    &amp;&amp; echo \"xdebug.remote_log='\/tmp\/xdebug_log\/xdebug.log'\" &gt;&gt; \/usr\/local\/etc\/php\/conf.d\/xdebug.ini<\/pre>\n<p>As you can see there are some xdebug settings. I have set remote port to 9001 and enabled xdebug logging so it will be easier to check if it works (don&#8217;t forget to turn it off later).<br \/>\nNow lets take a look at docker-compose.yml file:<\/p>\n<pre class=\"\">version: '2'\r\nservices:\r\n  nginx:\r\n    image: nginx:latest\r\n    ports:\r\n     - \"8080:80\"\r\n    volumes:\r\n     - .\/source:\/source\r\n     - .\/nginx.conf:\/etc\/nginx\/conf.d\/default.conf\r\n    depends_on:\r\n     - php\r\n  php:\r\n    build: .\/php\r\n    environment:\r\n       XDEBUG_CONFIG: \"remote_host=172.17.0.1\"\r\n    volumes:\r\n     - .\/source:\/source\r\n     - .\/logs:\/tmp\/xdebug_log\r\n  db:\r\n    image: mariadb:latest\r\n    volumes:\r\n       - db_data:\/var\/lib\/mysql\r\n    environment:\r\n      MYSQL_DATABASE: 'test'\r\n      MYSQL_USER: 'root'\r\n      MYSQL_PASSWORD: 'test'\r\n      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'\r\nvolumes:\r\n  db_data:<\/pre>\n<p>Important part here is to set xdebug remote host in\u00a0environment variables. To get correct IP address you can run <em>ifconfig<\/em> command and find what IP address is used by\u00a0<em>docker0<\/em> interface. Alternatively use\u00a0the following command in container:<\/p>\n<pre class=\"lang:sh decode:true\">\/sbin\/ip route|awk '\/default\/ { print $3 }'<\/pre>\n<p>Also set 777 permissions to l<em>ogs<\/em> folder.<\/p>\n<p>Now we can execute <em>docker-compose run<\/em> to build and run our services.<\/p>\n<p>Lets check if xdebug can connect to remote host using <em>netcat<\/em> tool. Leave this command running in console and navigate to some php script with <a href=\"https:\/\/chrome.google.com\/webstore\/detail\/xdebug-helper\/eadndfjplgieldjbigjakmdgkmoaaaoc\" target=\"_blank\">xdebug trigger<\/a>\u00a0turned on:<\/p>\n<pre class=\"lang:default decode:true \">netcat -l 9001<\/pre>\n<p>If you see something like this then it is ok:<\/p>\n<pre class=\"lang:default decode:true \">&lt;?xml version=\"1.0\" encoding=\"iso-8859-1\"?&gt;\r\n&lt;init xmlns=\"urn:debugger_protocol_v1\" xmlns:xdebug=\"http:\/\/xdebug.org\/dbgp\/xdebug\" fileuri=\"file:\/\/\/source\/info.php\" language=\"PHP\" xdebug:language_version=\"5.6.27\" protocol_version=\"1.0\" appid=\"6\" idekey=\"XDEBUG_ECLIPSE\"&gt;&lt;engine version=\"2.5.1\"&gt;&lt;![CDATA[Xdebug]]&gt;&lt;\/engine&gt;&lt;author&gt;&lt;![CDATA[Derick Rethans]]&gt;&lt;\/author&gt;&lt;url&gt;&lt;![CDATA[http:\/\/xdebug.org]]&gt;&lt;\/url&gt;&lt;copyright&gt;&lt;![CDATA[Copyright (c) 2002-2017 by Derick Rethans]]&gt;&lt;\/copyright&gt;&lt;\/init&gt;<\/pre>\n<p>If it is not &#8211; check xdebug log file.<\/p>\n<p>Next step is to create SSH<a href=\"http:\/\/blog.trackets.com\/2014\/05\/17\/ssh-tunnel-local-and-remote-port-forwarding-explained-with-examples.html\">-tunnel<\/a> and configure PhpStorm.<\/p>\n<p>On working computer run the following command to create\u00a0SSH-tunnel:<\/p>\n<pre class=\"lang:zsh decode:true \">ssh -R 9001:localhost:9001 ubuntu@your.remote.host.ip.address.here<\/pre>\n<p>In PhpStorm set debug port to 9001:<\/p>\n<p><a href=\"http:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/PreferencesPHPstorm.png\"><img loading=\"lazy\" class=\"aligncenter wp-image-100 size-full\" src=\"http:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/PreferencesPHPstorm.png\" alt=\"Preferences PhpStorm\" width=\"801\" height=\"496\" srcset=\"https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/PreferencesPHPstorm.png 801w, https:\/\/magebrew.com\/blog\/wp-content\/uploads\/2017\/03\/PreferencesPHPstorm-300x186.png 300w\" sizes=\"(max-width: 767px) 89vw, (max-width: 1000px) 54vw, (max-width: 1071px) 543px, 580px\" \/><\/a><\/p>\n<p>Now you should be able to debug your application in PhpStorm.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>No so long ago I&#8217;ve started using Docker as my primary\u00a0development environment. Before that I had\u00a0everything (WEB-server, PHP, MySql, etc) installed\u00a0on my Mac and being run natively. Docker brings more flexibility and it is just fun. So now my setup includes Mac OS with PhpStorm installed and\u00a0remote Ubuntu server running Docker containers needed for my &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/magebrew.com\/blog\/debugging-php-app-using-xdebug-and-remote-docker-php-container\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Debugging PHP app using Xdebug and Remote Docker PHP Container&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[16,17,18],"_links":{"self":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/94"}],"collection":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/comments?post=94"}],"version-history":[{"count":12,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions"}],"predecessor-version":[{"id":108,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/posts\/94\/revisions\/108"}],"wp:attachment":[{"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/media?parent=94"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/categories?post=94"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/magebrew.com\/blog\/wp-json\/wp\/v2\/tags?post=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}