Xdebug, Docker and VSCode on Windows 10 - php

I am looking for some guidance on setting up Xdebug with Docker and VSCode on Windows 10. My docker settings are as follows, but I always get 'EADDRINUSE: address already in use :::9000' and never get any output from Xdebug from inside VSCode!
The project is Laravel based.
.vscode\launch.json
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug on Docker App",
"type": "php",
"request": "launch",
"port": 9000,
// "pathMappings": {
// "/var/www/html": "${workspaceFolder}"
"pathMappings": {
"/var/www/html": "C:\\development\\inventory-service\\"
},
}
]
}
dockerfile
# Create and build composer dependencies
FROM composer:2.0 AS vendor
COPY database/ database/
COPY composer.json composer.json
COPY composer.lock composer.lock
RUN composer install \
--ignore-platform-reqs \
--no-interaction \
--no-plugins \
--no-scripts \
--prefer-dist
# Create and build application
FROM php:8.0-apache
ENV APACHE_DOCUMENT_ROOT=/var/www/html/public
RUN sed -ri -e 's!/var/www/html!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/sites-available/*.conf \
&& sed -ri -e 's!/var/www/!${APACHE_DOCUMENT_ROOT}!g' /etc/apache2/apache2.conf /etc/apache2/conf-available/*.conf
# RUN mv "$PHP_INI_DIR/php.ini-development" "$PHP_INI_DIR/php.ini"
RUN apt-get update && apt-get install -y \
libpng-dev \
zlib1g-dev \
libxml2-dev \
libzip-dev \
libonig-dev \
zip \
curl \
unzip \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install zip \
&& docker-php-source delete
# Install Xdebug
# RUN pecl install -f xdebug
# RUN echo "zend_extension=$(find /usr/local/lib/php/extensions/ -name xdebug.so)" > /usr/local/etc/php/conf.d/xdebug.ini;
RUN pecl install xdebug && docker-php-ext-enable xdebug \
&& echo "\n\
xdebug.remote_host = 192.168.150.1 \n\
xdebug.default_enable = 1 \n\
xdebug.remote_autostart = 1 \n\
xdebug.remote_connect_back = 0 \n\
xdebug.remote_enable = 1 \n\
xdebug.remote_handler = "dbgp" \n\
xdebug.remote_port = 9000 \n\
xdebug.remote_log = /var/www/html/xdebug.log \n\
" >> /usr/local/etc/php/conf.d/docker-php-xdebug.ini
# Copy the scheduler script into the image
COPY scheduler.sh /usr/local/bin/scheduler
COPY service.sh /usr/local/bin/service
# Set permissions on image folders
RUN a2enmod rewrite headers \
&& chmod u+x /usr/local/bin/scheduler \
&& chmod u+x /usr/local/bin/service
# Copy the application into the image
COPY --from=vendor /usr/bin/composer /usr/bin/composer
COPY --from=vendor /app/vendor/ /var/www/html/vendor/
COPY . /var/www/html
# Set permissions on image folders
RUN chown -R www-data:www-data /var/www/html
I've gone through a number of posts online to set this up but I always seem to have the same issue of port usage and/or no debug data (despite VSCode running the debugger).
Any help much appreciated...

OK, so there was 1 file I needed to update (dockerfile) and I needed to create a new file (.vscode\launch.json).
Add the following to dockerfile (port number used by Xdebug is 9003)
# Install Xdebug
RUN pecl install xdebug && docker-php-ext-enable xdebug \
&& echo "\n\
xdebug.client_host=host.docker.internal \n\
xdebug.mode=debug \n\
xdebug.start_with_request=yes \n\
xdebug.discover_client_host=0 \n\
xdebug.remote_handler = "dbgp" \n\
xdebug.client_port=9003 \n\
xdebug.log = /var/www/html/xdebug.log \n\
" >> /usr/local/etc/php/conf.d/docker-php-xdebug.ini
And then create or update your .vscode\launch.json (pathMappings: path on Docker then local path to your code folder)
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug on Docker App",
"type": "php",
"request": "launch",
"port": 9003,
"pathMappings": {
"/var/www/html": "C:\\development\\inventory-service\\"
},
}
]
}
...then rebuild your docker instance.
Debugging in browser:
From VSCode, select file for debugging, add break points where needed, then hit F5 to start debugging session. Then go to the page you are debugging, locally, from your web browser.
Debugging artisan commands via console:
From VSCode, select file for debugging, add break points where needed, then hit F5 to start debugging session. Then run the console command from the command line directly (in the normal manner) on the docker instance (choose the CLI from docker desktop for the relevant container/service).
I believe there is a way to test console from your VSCode terminal, but it never worked for me!

Related

Debug PHP with Xdebug in Docker with VSCode on Mac

I am running a Symfony application inside a Docker-Container.
I want to debug the code with VSCode on my mac. With Windows everything works fine.
The debugger is connecting to the container, but does not stop at the breakpoints.
This is my launch.json
{
"version": "0.1.0",
"configurations": [{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9003,
"log": true,
"externalConsole": false,
"pathMappings": {
"/srv/app": "${workspaceFolder}",
"/srv/cssp/src/WorkingBundle": "${workspaceFolder}/src/WorkingBundle"
},
"ignore": [
"**/vendor/**/*.php"
]
},
]
In the php.ini I added
xdebug.mode=debug
xdebug.client_host=host.docker.internal
xdebug.start_with_request=trigger
My Dockerfile
FROM composer:latest AS composer
FROM php:7.2-apache-stretch
COPY --chown=33:33 . /srv/cssp
COPY .docker/vhost.conf /etc/apache2/sites-available/000-default.conf
COPY .docker/php.ini /usr/local/lib/
COPY .docker/php.ini /usr/local/lib/php
COPY .docker/php.ini /usr/local/etc/php
WORKDIR /srv/symfonyApp
ENV COMPOSER_ALLOW_SUPERUSER 1
COPY --from=composer /usr/bin/composer /usr/bin/composer
RUN chown -R www-data:www-data /srv/symfonyApp\
&& apt-get update \
&& apt-get -y install libpng-dev libjpeg-dev unzip\
&& docker-php-ext-install -j$(nproc) mbstring mysqli pdo pdo_mysql shmop zip gd \
&& a2enmod rewrite ssl socache_shmcb \
&& service apache2 restart \
&& composer install \
&& chown -R www-data:www-data /srv/symfonyApp\
&& useradd -rm -d /home/symfonyApp -s /bin/bash -g root -G sudo -u 503 cssp \
&& pecl install xdebug \
&& apt install nano \
&& docker-php-ext-enable xdebug \
&& apt-get install libfontconfig1 libxrender1 libxtst6
And my docker-compose.yml
version: '3.8'
services:
app:
build:
context: .
dockerfile: .docker/Dockerfile
image: symfonyApp-docker
ports:
- 8080:80
links:
- mysql
volumes:
- .:/srv/symfonyApp
environment:
PHP_IDE_CONFIG: 'serverName=localhost'
XDEBUG_SESSION: 'VSCODE'
mysql:
image: mariadb:10.4.4
container_name: symfonyApp_mysql
volumes:
- .docker/mysql/init:/docker-entrypoint-initdb.d
restart: always
ports:
- 13306:3306
environment:
MYSQL_ROOT_PASSWORD: secret

Can not set up Xdebug for any IDE using Laradock + PHP 7.2 + PhpStorm/VSCode + Xdebug 2.9.8/Xdebug 3.0.3 working with Laravel project from WSL

I have a big issue for setting up Xdebug in my Laravel project + Laradock.
I have tried with PhpStorm and also with VSCode.
I also have tried using Xdebug 3, writing the same settings on both xdebug.ini but with the new Xdebug3 sentences.
Maybe should I have to set up something else inside WSL ?
I have tried with a lot of combinations on my xdebug.ini but without success.
Below my configurations file:
php-fpm/xdebug.ini
xdebug.remote_host="host.docker.internal"
xdebug.remote_connect_back=0
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.cli_color=1
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1
php-workspace/xdebug.ini
xdebug.remote_host="host.docker.internal"
xdebug.remote_connect_back=0
xdebug.remote_port=9000
xdebug.idekey=PHPSTORM
xdebug.remote_autostart=1
xdebug.remote_enable=1
xdebug.cli_color=1
xdebug.profiler_enable=0
xdebug.profiler_output_dir="~/xdebug/phpstorm/tmp/profiling"
xdebug.remote_handler=dbgp
xdebug.remote_mode=req
xdebug.var_display_max_children=-1
xdebug.var_display_max_data=-1
xdebug.var_display_max_depth=-1
LARADOCK FOLDER
...
WORKSPACE_INSTALL_XDEBUG=true
...
PHP_FPM_INSTALL_XDEBUG=true
...
# Create an account on blackfire.io. Don't enable blackfire and xDebug at the same time. # visit https://blackfire.io/docs/24-days/06-installation#install-probe-debian for more info.
INSTALL_BLACKFIRE=false
LARADOCK/PHP-FPM/Dockerfile
###########################################################################
# xDebug:
###########################################################################
ARG INSTALL_XDEBUG=false
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
# Install the xdebug extension
if [ $(php -r "echo PHP_MAJOR_VERSION;") = "5" ]; then \
pecl install xdebug-2.5.5; \
else \
if [ $(php -r "echo PHP_MINOR_VERSION;") = "0" ]; then \
pecl install xdebug-2.9.0; \
else \
pecl install xdebug-2.9.8; \
fi \
fi && \
docker-php-ext-enable xdebug \
;fi
# Copy xdebug configuration for remote debugging
COPY ./xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini
RUN sed -i "s/xdebug.remote_autostart=0/xdebug.remote_autostart=1/" /usr/local/etc/php/conf.d/xdebug.ini && \
sed -i "s/xdebug.remote_enable=0/xdebug.remote_enable=1/" /usr/local/etc/php/conf.d/xdebug.ini && \
sed -i "s/xdebug.cli_color=0/xdebug.cli_color=1/" /usr/local/etc/php/conf.d/xdebug.ini
LARADOCK/WORKSPACE/Dockerfile
###########################################################################
# xDebug:
###########################################################################
USER root
ARG INSTALL_XDEBUG=false
RUN if [ ${INSTALL_XDEBUG} = true ]; then \
# Install the xdebug extension
if [ $(php -r "echo PHP_MAJOR_VERSION;") = "5" ]; then \
pecl install xdebug-2.5.5; \
else \
if [ $(php -r "echo PHP_MAJOR_VERSION;") = "7" ] && [ $(php -r "echo PHP_MINOR_VERSION;") = "0" ]; then \
pecl install xdebug-2.9.0; \
else \
if [ $(php -r "echo PHP_MAJOR_VERSION;") = "7" ] && [ $(php -r "echo PHP_MINOR_VERSION;") = "1" ]; then \
pecl install xdebug-2.9.8; \
else \
if [ $(php -r "echo PHP_MAJOR_VERSION;") = "7" ]; then \
pecl install xdebug-2.9.8; \
else \
#pecl install xdebug; \
echo "xDebug 3 required, not supported."; \
fi \
fi \
fi \
fi && \
echo "zend_extension=xdebug.so" >> /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/20-xdebug.ini \
;fi
# ADD for REMOTE debugging
COPY ./xdebug.ini /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini
RUN sed -i "s/xdebug.remote_autostart=0/xdebug.remote_autostart=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini && \
sed -i "s/xdebug.remote_enable=0/xdebug.remote_enable=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini && \
sed -i "s/xdebug.cli_color=0/xdebug.cli_color=1/" /etc/php/${LARADOCK_PHP_VERSION}/cli/conf.d/xdebug.ini
laradock/docker-compose.yml
### Workspace Utilities ##################################
workspace:
build:
context: ./workspace
args:
...
- INSTALL_XDEBUG=${WORKSPACE_INSTALL_XDEBUG}
...
### PHP-FPM ##############################################
php-fpm:
build:
context: ./php-fpm
args:
...
- INSTALL_XDEBUG=${PHP_FPM_INSTALL_XDEBUG}
...
PHPSTORM SETTINGS
WINDOWS SETTINGS
PHPINFO()

Xdebug on VSCode for Docker contained PHP app, no stopping on breakpoints

I'm sorry in advance, I did my research and I noticed the same question multiple times on this page, I've been trying to figure it out by myself but I can't quite comprehend how to get this working with my specific configuration
This is a project from work which has this Dockerfile:
FROM php:7.3-apache
RUN apt-get update && apt-get install -y acl unzip \
&& rm -r /var/lib/apt/lists/*
COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/bin/
RUN install-php-extensions gd intl pdo_mysql soap xdebug zip
RUN echo "xdebug.remote_enable=1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_port=10000" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini \
&& echo "xdebug.remote_host=172.17.0.1" >> /usr/local/etc/php/conf.d/docker-php-ext-xdebug.ini
WORKDIR /var/www/app
COPY --from=composer:1.10 /usr/bin/composer /usr/bin/composer
COPY docker/000-default.conf /etc/apache2/sites-enabled/000-default.conf
COPY docker/app.conf /etc/apache2/conf-enabled/z-app.conf
COPY docker/app.ini $PHP_INI_DIR/conf.d/app.ini
RUN ln -s $PHP_INI_DIR/php.ini-development $PHP_INI_DIR/php.ini
RUN a2enmod rewrite
It's supposed to already have Xdebug working, no?
I installed the php debug extension on VSCode and this is my launch.json config:
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 10000,
"pathMappings": {
"/var/www/app": "${workspaceFolder}"
},
}
I start the debugging session but the project doesn't stop on breakpoints, so I'm just lost
I hope you guys can help me out, thanks!

I can't debbug my php application running in a docker container on my linux machine with Xdebug using Visual Studio Code

I'm trying to set up an development environment with docker to run an CakePHP application and debbug it using Xdebug. Aparentely, Xdebug configuration is alright, but it can't connect back to my IDE either because of some misconfiguration of my lauch.json or because of the xdebug.remote_host=host.docker.internal directive in my xdebug.ini that may not be working properly in my Linux machine, even after I putted RUN ip -4 route list match 0/0 | awk '{print $3 " host.docker.internal"}' >> /etc/hosts in my dockerfile, but the result of this command is never an entry mapping to host.docker.internal, instead it returns a series of numbers like so: 172.26.0.3 24577f456543
.
I already tried to put the ip address of my linux machine witch is hosting the docker container on the xdebug.remote_host directive in xdebug.ini, but it still didin't work. Unfortunetelly I couldn't get the `xdebug.remote_log' working as well, witch may be a sign that I can be doing something wrong in that point too.
Dockerfile:
FROM ubuntu:14.04
ARG DEBIAN_FRONTEND=noninteractive
USER root
#####################################
# Non-Root User:
#####################################
# Add a non-root user to prevent files being created with root permissions on host machine.
ARG PUID=1000
ARG PGID=1000
ENV PUID ${PUID}
ENV PGID ${PGID}
RUN groupadd -g ${PGID} ubuntu && \
useradd -u ${PUID} -g ubuntu -m ubuntu && \
apt-get update -yqq
# RUN apt-get install -y software-properties-common && \
# add-apt-repository -y ppa:ondrej/php
# PHP 5 AND EXTENSIONS INSTALL
RUN apt-get update && \
apt-get install -y \
php5-cli \
php5-common \
php5-curl \
php5-json \
# php5-xml \
# php5-mbstring \
php5-mcrypt \
php5-mysql \
# php5-sqlite3 \
# php5-zip \
# php5-bcmath \
php5-gd \
php5-dev \
pkg-config \
libcurl4-openssl-dev \
libedit-dev \
libssl-dev \
libxml2-dev \
xz-utils \
libsqlite3-dev \
git \
curl \
vim \
nano \
&& apt-get clean
# APACHE2 AND APACHE_PHP 5 INSTALL with rewrite enabled
RUN apt-get update -q && apt-get install -yqq --force-yes \
apache2 \
libapache2-mod-php5 \
&& apt-get clean \
&& a2enmod rewrite
# ENABLE MCRYPT
RUN php5enmod mcrypt
# Source the bash
RUN . ~/.bashrc
# ADD DEFAULT VHOST
COPY ./docker/000-default.conf /etc/apache2/sites-available/000-default.conf
#####################################
# xDebug:
#####################################
USER root
RUN apt-get install -y --force-yes php5-xdebug && \
sed -i 's/^;//g' /etc/php5/cli/conf.d/20-xdebug.ini && \
echo "alias phpunit='php -dzend_extension=xdebug.so /var/www/vendor/bin/phpunit'" >> ~/.bashrc
RUN ip -4 route list match 0/0 | awk '{print $3 " host.docker.internal"}' >> /etc/hosts
# ADD for REMOTE debugging
COPY ./docker/xdebug.ini /etc/php5/cli/conf.d/xdebug.ini
#####################################
# NODE INSTALL:
#####################################
USER ubuntu
# Check if NVM needs to be installed
ARG NODE_VERSION=6.9
ENV NVM_DIR /home/ubuntu/.nvm
RUN curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.1/install.sh | bash && \
. $NVM_DIR/nvm.sh && \
nvm install ${NODE_VERSION} && \
nvm use ${NODE_VERSION} && \
nvm alias ${NODE_VERSION}
# Wouldn't execute when added to the RUN statement in the above block
# Source NVM when loading bash since ~/.profile isn't loaded on non-login shell
RUN echo "" >> ~/.bashrc && \
echo 'export NVM_DIR="$HOME/.nvm"' >> ~/.bashrc && \
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> ~/.bashrc
# Add NVM binaries to root's .bashrc
USER root
RUN echo "" >> ~/.bashrc && \
echo 'export NVM_DIR="/home/ubuntu/.nvm"' >> ~/.bashrc && \
echo '[ -s "$NVM_DIR/nvm.sh" ] && . "$NVM_DIR/nvm.sh" # This loads nvm' >> ~/.bashrc
#####################################
# TESSERACT INSTALL:
#####################################
RUN apt-get install tesseract-ocr -y
# Clean up
RUN apt-get clean && \
rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
# Set default work directory
WORKDIR /var/www/html
xdebug.ini:
; Defaults
xdebug.default_enable=1
xdebug.remote_enable=1
xdebug.remote_port=9001
xdebug.remote_host=host.docker.internal
xdebug.remote_handler=dbgp
; The Linux way
xdebug.remote_connect_back=1
; idekey value is specific to Visual Studio Code
xdebug.idekey=VSCODE
; Optional: Set to true to always auto-start xdebug
xdebug.remote_autostart=true
docker-compose.yml:
version: '3.1'
services:
smarty-center:
build:
dockerfile: ./docker/Dockerfile
context: .
ports:
- "80:80"
links:
- mysql56:mysql
volumes:
- .:/var/www/html
container_name: app
networks:
- homologacao-network
depends_on:
- mysql56
phpmyadmin:
image: phpmyadmin/phpmyadmin
environment:
- MYSQL_ROOT_PASSWORD=nopass
ports:
- "8000:80"
links:
- mysql56:db
container_name: phpmyadmin
networks:
- homologacao-network
depends_on:
- mysql56
mysql56:
image: mysql:5.6
environment:
- MYSQL_ROOT_PASSWORD=nopass
volumes:
- ./docker/mysql:/var/lib/mysql
container_name: mysql56
networks:
- homologacao-network
ports:
- "3306:3306"
networks:
homologacao-network:
driver: bridge
lauch.json
{
// Use IntelliSense to learn about possible attributes.
// Hover to view descriptions of existing attributes.
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
"version": "0.2.0",
"configurations": [
{
"name": "Xdebug for Project smarty center",
"type": "php",
"request": "launch",
"port": 9001,
"log": true,
"pathMappings": {
"/var/www/html": "${workspaceRoot}"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001
}
]
}
I expect Xdebug working properly to debbug my application running in a container.

docker-php-ext-install - sources still not found despite extension is installed

I am trying to install extenion in the docker PHP image. I have the following Dockerfile:
FROM php:7.2-apache
RUN apt-get update
RUN apt-get install -y build-essential cmake g++ libboost-dev libboost-system-dev \
libboost-filesystem-dev libexpat1-dev zlib1g-dev libxml2-dev \
libbz2-dev libpq-dev libproj-dev \
git sudo wget postgresql-server-dev-all python-pyosmium osmium-tool
RUN docker-php-ext-install dba
RUN docker-php-ext-install pgsql
RUN printf '<Directory "/srv/nominatim/Nominatim/build/website"> \n\
Options FollowSymLinks MultiViews \n\
AddType text/html .php \n\
DirectoryIndex search.php \n\
Require all granted \n\
</Directory> \n\
\n\
Alias /nominatim /srv/nominatim/Nominatim/build/website \n' > /etc/apache2/conf-available/nominatim.conf
RUN a2enconf nominatim
RUN service apache2 stop
RUN service apache2 start
WORKDIR /srv/nominatim
RUN wget https://nominatim.org/release/Nominatim-3.2.0.tar.bz2
RUN tar xf Nominatim-3.2.0.tar.bz2
RUN mv Nominatim-3.2.0 Nominatim
WORKDIR /srv/nominatim/Nominatim
RUN mkdir build
WORKDIR /srv/nominatim/Nominatim/build
RUN cmake ..
RUN make
RUN tee settings/local.php << $"EOF \n\
<?php \n\
#define('CONST_Website_BaseURL', '/nominatim/'); \n\
EOF"
The problem is that despite extensions are installed and their ini files are listed by phpinfo, the extension doesn't work:
Warning: require_once(DB.php): failed to open stream: No such file or directory in /srv/nominatim/Nominatim/lib/db.php on line 3
Fatal error: require_once(): Failed opening required 'DB.php' (include_path='.:/usr/local/lib/php') in /srv/nominatim/Nominatim/lib/db.php on line 3
It seems like the DB.php indeed does not exist at all (or I couldn't find them), despite that the extension seems to be installed - as the .so file is in its place as well.
EDIT: I've pasted the full Dockerfile above. I am actually trying to build the Nominatim image that will be composed with another image containing the PostgreSQL with the OSM data.

Categories