How to Setup Docker + PHPstorm + Xdebug on Ubuntu 16.04

How to setup Docker + PhpStorm + xdebug on Ubuntu 16.04

I found out solution how to run xdebug.

1) First of we need create a new static route that will based on your network device. For create new static route: run in terminal ifconfig

and found out the exists network device. In my situation name of device will be as

wlp4s0

Sample Image

2) Go ahead. Let's begin create the static route. In terminal run command like this:

> sudo ip addr add 10.254.254.254/24 brd + dev wlp4s0 label wlp4s0:1

3) Now if you run again ifconfig you'll see new static route:
Sample Image

4) Update

xdebug.ini

file add:

xdebug.remote_host=10.254.254.254

5) Update docker.compose.yml file:
in php section add:

environment:
PHP_IDE_CONFIG: "serverName=project-docker"
PHP_XDEBUG_ENABLED: 1
XDEBUG_CONFIG: remote_host=10.254.254.254

6) The last thing is update phpstorm settings.

Server settings:
phpstorm settings
Remote debug config:
phpstorm settings
7) And profit xdebug is working:
xdebug is working

Docker, PhpStorm & Xdebug: Can't find source position error

That happend because on Servers section of the phpstorm does not have the very same name as defined in the PHP_IDE_CONFIG enviromental variable in order to solve that follow these steps:

Step 1:
Go to server settings by pressing Ctrl+Alt+S or by visiting File -> Settings from the menu.

Step 2:
Then on the open window go to Settings -> Languages & Framework -> Servers.

Step 3:
Set the Name as the one defined into:

export PHP_IDE_CONFIG="serverName=0.0.0.0:5092"

In order to work it should be 0.0.0.0:5092 as the image shows:

Server Setting here

PhpStorm cannot start Xdebug (to work with Docker)

Somehow the docker container cannot access port 9000 on host machine. By running the following command I fixed the problem:

# iptables -I INPUT -p tcp -m tcp --dport 9000 -j ACCEPT

Can't connect PhpStorm with xdebug with Docker

In the end I had to do put the following over my Dockerfile:

FROM php:7.0-fpm-alpine

VOLUME /var/log/xdebug

ARG XDEBUG_HOST="172.17.0.1"
ARG XDEBUG_PORT=9021

RUN apk add --update --virtual build_deps gcc g++ autoconf make &&\
docker-php-source extract &&\
pecl install xdebug &&\
docker-php-ext-enable xdebug &&\
&& echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/xdebug.ini \
&& echo "xdebug.remote_autostart=0" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
echo "xdebug.remote_connect_back=1" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
echo "xdebug.remote_handler = dbgp" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
echo "xdebug.remote_mode = req" >> /usr/local/etc/php/conf.d/xdebug.ini &&\
echo "xdebug.remote_host=${XDEBUG_HOST}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
echo "xdebug.remote_port=${XDEBUG_PORT}" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini &&\
docker-php-source delete && \
apk del build_deps && \
rm -rf /var/cache/apk/* && \
rm -rf /tmp/*

ENTRYPOINT ["php-fpm"]

The XDEBUG_HOST build arg contains the ip of docker0 network interface over GNU/Linux systems (use ifconfig to find out).

And over my docker-compose.yml I provide the following:

version: '2'
services:
nginx_dev:
image: nginx:alpine
ports:
- "5092:5092"
links:
- "my_symfony_www_dev"
volumes:
- './conf/nginx/nginx_dev.conf:/etc/nginx/nginx.conf:ro'
- './logs/dev:/var/logs'
volumes_from:
- 'my_symfony_www_dev'

my_symfony_www_dev:
build:
context: .
dockerfile: Dockerfile_dev
args:
XDEBUG_HOST: 172.17.0.1
XDEBUG_PORT: 9021
image: "myimage/my_symfony_app:dev_php"
volumes:
- "$SOURCE_PATH:/var/www/html:Z"

Over the ./conf/nginx/nginx_dev.conf mapped over the volume I put the following setting on server section:

server_name 0.0.0.0;

Then on phpstorm use the following settings:

Setting Servers over phpstorm

XDEBUG settings over phpstorm

Then you are good to go!

Docker Compose and Xdebug not working in PhpStorm

After booting up my machine to investigate further with the comments of @abestrad and @LazyOne, without changing anything, opening localhost:8080 suddenly let the debugging work by stoping at the breakpoint that i have set. Actually I had already tried to restart the Docker Desktop App before writing the question, maybe at that point my configurations were at a wrong state.

But at the end the solution was: Restarting the PC.

Watch out

To be sure i tried to open it also in a private browser session and it wasn't working anymore. That was because the special cookie still was set in the normal browser store (cookie that was stored either from the Browser extension that I have already uninstalled, or from trying out the JetBrains Bookmarklets generator before writing the question).

The solution to let it work everytime was to add following:

xdebug.remote_autostart=1

Citate from here:

Normally you need to use a specific HTTP GET/POST variable to start remote debugging (see Step Debugging). When this setting is set to 1, Xdebug will always attempt to start a remote debugging session and try to connect to a client, even if the GET/POST/COOKIE variable was not present.

Docker+php-fpm+Xdebug (on Linux) + PhpStorm (on Windows)

Thanks to all. The problem turned out to be in the firewall. I opened port 9000 and it worked

How to setup xdebug and PhpStorm with docker for Windows (beta)

i've finally got it to work ! the key was to set network_mode to host :

https://docs.docker.com/compose/compose-file/

docker-compose.yml :

version: '2'

services:
web:
build: php5
ports:
- "80:80"
#links:
# - db:db
network_mode: "host"
db:
image: mysql
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=root

after trying this i noticed that my container had an interface IPed 192.168.65.2

so i telneted 192.168.65.1 9000 and it worked !

php.ini :

xdebug.idekey=PHPSTORM
xdebug.remote_port=9000
xdebug.remote_host=192.168.65.1

i've selected "Expose container ports on localhost" (new option) in docker settings.

i can't use links any more, because of the specified network_mode. so i've opened port 3306 and i have to choose 192.168.65.1 as mysql host. i will probably find some workaround about this, but finally it works !

Phpstorm Xdebug not working with ddev

It wasn't a DDEV-problem, it was indeed the firewall causing the issue.
I created a rule for ufw:

 sudo ufw allow in on docker0 from 172.17.0.0/24 to 172.17.0.1/32 port 9000 comment xdebug

and a rule for iptables:

sudo iptables -I INPUT -p tcp -m tcp --dport 9000 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 9000 -j ACCEPT
You also need to make sure the rule is loaded at boot.

@rfay ,thanks for the hint.



Related Topics



Leave a reply



Submit