So, after hours of Googling no right answer is found. Ive started the Laravel 8.x tutorial installation for Windows using Docker and Laravel’s sail.
Now i want to use Xdebug and have absolutely no idea what to do.
in the root of the project there is a docker-compose.yml (this is the default)
# For more information: https://laravel.com/docs/sail
version: '3'
services:
laravel.test:
build:
context: ./vendor/laravel/sail/runtimes/8.0
dockerfile: Dockerfile
args:
WWWGROUP: '${WWWGROUP}'
image: sail-8.0/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
PHP_IDE_CONFIG: 'serverName=localhost'
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
- redis
# - selenium
# selenium:
# image: 'selenium/standalone-chrome'
# volumes:
# - '/dev/shm:/dev/shm'
# networks:
# - sail
# depends_on:
# - laravel.test
mysql:
image: 'mysql:8.0'
ports:
- '${DB_PORT}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
redis:
image: 'redis:alpine'
ports:
- '${REDIS_PORT}:6379'
volumes:
- 'sailredis:/data'
networks:
- sail
# memcached:
# image: 'memcached:alpine'
# ports:
# - '11211:11211'
# networks:
# - sail
mailhog:
image: 'mailhog/mailhog:latest'
ports:
- 1025:1025
- 8025:8025
networks:
- sail
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
sailredis:
driver: local
And in the vendor/laravel/sail/runtimes/8.0 folder there is a Dockerfile
FROM ubuntu:20.04
LABEL maintainer="Taylor Otwell"
ARG WWWGROUP
WORKDIR /var/www/html
ENV DEBIAN_FRONTEND noninteractive
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apt-get update
&& apt-get install -y gnupg gosu curl ca-certificates zip unzip git supervisor sqlite3 libcap2-bin
&& mkdir -p ~/.gnupg
&& echo "disable-ipv6" >> ~/.gnupg/dirmngr.conf
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys E5267A6C
&& apt-key adv --homedir ~/.gnupg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C300EE8C
&& echo "deb http://ppa.launchpad.net/ondrej/php/ubuntu focal main" > /etc/apt/sources.list.d/ppa_ondrej_php.list
&& apt-get update
&& apt-get install -y php8.0-cli php8.0-dev
php8.0-pgsql php8.0-sqlite3 php8.0-gd
php8.0-curl php8.0-memcached
php8.0-imap php8.0-mysql php8.0-mbstring
php8.0-xml php8.0-zip php8.0-bcmath php8.0-soap
php8.0-intl php8.0-readline
php8.0-msgpack php8.0-igbinary php8.0-ldap
php8.0-redis
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer
&& curl -sL https://deb.nodesource.com/setup_15.x | bash -
&& apt-get install -y nodejs
&& apt-get -y autoremove
&& apt-get clean
&& rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
RUN setcap "cap_net_bind_service=+ep" /usr/bin/php8.0
RUN groupadd --force -g $WWWGROUP sail
RUN useradd -ms /bin/bash --no-user-group -g $WWWGROUP -u 1337 sail
COPY start-container /usr/local/bin/start-container
COPY supervisord.conf /etc/supervisor/conf.d/supervisord.conf
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]
now what?
I’m on a Windows system
I want to connect it to PhpStorm
6
Answers
The underlying dockerfile can be found here.
In order to run xdebug you need to install the PHP extension.Personally I like to use docker-php-extension-installer to install them.
Add these lines after line 35:
Add these lines after line 35:
After that you need to configure Xdebug, PhpStorm and your browser.
xdebug.ini
both the remote_port and the remote_host are very important.
Make sure this port does not conflict with a service inside the container, in this case do not use 8000 as PHP already runs on this port.
You need to create a server first under:
File > Settings > Languages & Frameworks > PHP > Servers
It is very important to correctly map the paths in this case:
.:/var/www/html
Which means your project root directory on the left should map to
/var/www/html
Now it is time to create a run config. You can do this in the top right corner of PhpStorm under "Add Configuration…"
Use the screenshot as a guide.
Also do the same inside PhpStorm in the right corner.
I’m in the same situation as OP and didn’t find a solution, but this is what I found:
If you’ve installed xdebug via command line recently, chances are you’ve installed Xdebug V3 which has different config options than Xdebug V2. It doesn’t have remote_autostart or remote_enable options so setting those will have no effect.
You can read more about Xdebug 3 config options here: https://xdebug.org/docs/all_settings
I had the same problem for VS Code on a Linux workstation.
Maybe my solution could work for you, too.
Apparently, XDebug is not installed at all with Laravel Sail. In order to include it, you have to modify the
Dockerfile
, editdocker-compose.yml
and rebuild the containers.Here is how I did.
cp -r vendor/laravel/sail/runtimes/8.0 ./resources/docker/
context
and added a variable underargs
in the first lines ofdocker-compose.yml
:So the
context
points where I copied the original Docker configuration. I also decided to bind the newXDEBUG
arg to the value of theAPP_DEBUG
variable inside the.env
file, in order to switch off XDebug in a production environment.Dockerfile
I copied before in order to include xdebug when building the containers. The script should also write the correct configuration options for Xdebug 3 insidephp.ini
:You can find out if XDebug is running:
For VSCode only:
Inside
Preferences -> Settings
underDebug
you should check "Debug: Allow breakpoints everywhere".Change the default
launch.json
file:I got XDebug to work with a web request after some help from the posters above.
I have quite a bit of experience with PHPStorm and XDebug 2.x. I am new to docker so there may be better ways to fix this.
I have not yet figured out how to run or debug tests that rely on the database connection from inside PHPStorm (right click on a test to debug). They run successfully with breakpoints if I am "listening" tosail test
(which will run the tests correctly) but PHPStorm can’t find the MySQL database when running tests and I also get this error: "Xdebug: [Step Debug] Could not connect to debugging client. Tried: host.docker.internal:9003"Update 1/18/21 to fix local environment within PHPStorm so that it can find the database and the rest of the Docker network. Now I can successfully run tests and debug from within PHPStorm. With the current build of PHPStorm 2020.3.1 You need to add the network name where it asks for the network mode. I will be reporting this to them so it may get addressed soon.
Get your network name by running
docker network ls
. In this case it is myProjectName_sail.Enter this in PHPStorm Preferences>PHP>CLI Interpreter>…
On to the fix for debugging web requests:
What I have:
What I did:
RUN apt-get -y install vim
docker exec -it mySite.test_1 vim /etc/php/8.0/cli/php.ini
.You need to get your
/etc/php/8.0/cli/php.ini
file to look like this (client_host was the key):I added this to the Dockerfile as recommended by @Enea74. I was having trouble getting the conditional to return TRUE so I hard coded
TRUE
here for now:sail build --no-cache
sail up -d
sail php -v
returns:
Now In PHPStorm do the following. I think that it discovered many of these settings on its own: I don’t remember setting all of these values:
Hopefully this will help someone.
There is a discarded PR for optional Xdebug 3.0 support in laravel/sail (PHP 7.4 and PHP 8). Please follow the discussion.
Check the commit, what to change in
Dockerfile
anddocker-compose.yml.
Don’t forget to set the variables in your .envMy contribution to @Enea74 and @smenzer great answers above:
I successfully configured Sail with xdebug working on VSCODE on linux mint.
But when I run some phpUnit or sail test, it was not working as expected and received this message
After a long search, I figured out that need to add this line on the Dockerfile of Sail after the apt install commands:
Rebuild the containers with
And then the xdebug start to work with phpunit tests on shell along with normal browser navigation.
Note: if the command ‘ip’ is not working, add the package ‘iproute2’ on previous apt install commands.