Debug PHP with Xdebug in Docker with VSCode on Mac - php

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

Related

Laravel SAIL_XDEBUG_MODE not working for debugging web calls

I have started to play with my first Laravel project on a MacOS. I am using Laravel Sail for running the project inside a container and everything seems to work except the debugging part.
Versions used:
Laravel 8.66,
PHP 8.0.12,
Xdebug 3.1.1
According to Laravel docs we can set SAIL_XDEBUG_MODE to enable the debug mode. However, the debugging is not working in PhpStorm even if it is set to listen for incoming connections. I have seen that running php -i in container returns different configs for Xdebug comparing to what we see in phpinfo() from browser even if the Configuration file is the same one.
Here are some screenshots from browser:
Here are screenshots from what I see in the command line:
As it can be seen in the attached screenshots, in console is used the XDEBUG_MODE env var, while in browser is used xdebug.mode. Is is possible to have the same configs in console and browser via SAIL_XDEBUG_MODE ?
This is the Dockerfile that is used:
FROM ubuntu:21.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 libpng-dev python2 \
&& mkdir -p ~/.gnupg \
&& chmod 600 ~/.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 hirsute 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-pcov \
php8.0-msgpack php8.0-igbinary php8.0-ldap \
php8.0-redis php8.0-swoole php8.0-xdebug \
&& php -r "readfile('http://getcomposer.org/installer');" | php -- --install-dir=/usr/bin/ --filename=composer \
&& curl -sL https://deb.nodesource.com/setup_16.x | bash - \
&& apt-get install -y nodejs \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get install -y yarn \
&& apt-get install -y mysql-client \
&& apt-get install -y postgresql-client \
&& 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"]
And this is my docker-compose.yml:
# 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
extra_hosts:
- 'host.docker.internal:host-gateway'
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: '${WWWUSER}'
LARAVEL_SAIL: 1
XDEBUG_MODE: '${SAIL_XDEBUG_MODE:-off}'
XDEBUG_CONFIG: '${SAIL_XDEBUG_CONFIG:-client_host=host.docker.internal}'
PHP_IDE_CONFIG: "serverName=my.local"
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
ports:
- '${FORWARD_DB_PORT:-3306}: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
healthcheck:
test: ["CMD", "mysqladmin", "ping", "-p${DB_PASSWORD}"]
retries: 3
timeout: 5s
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
As it can be seen in the attached screenshots, in console is used the XDEBUG_MODE env var, while in browser is used xdebug.mode.
Xdebug uses XDEBUG_MODE if it is set, otherwise it falls back to the value of the xdebug.mode setting.
I don't know which web server Sail uses, but some will strip out the environment variables — including XDEBUG_MODE.
So you can either fix that, or update the Dockerfile to add a line below:
COPY php.ini /etc/php/8.0/cli/conf.d/99-sail.ini
Which says:
COPY xdebug.ini /etc/php/8.0/cli/conf.d/999-xdebug.ini
And add to the contents of xdebug.ini (in the same directory as php.ini):
xdebug.mode=develop,debug
I had the same problem and I'm also on macOS.
You must use the export command, like this:
export SAIL_XDEBUG_MODE=develop,debug

docker entrypoint sh file restarting

I am testing docker with my php project. Everything is ok in testing but if I add ENTRYPOINT, docker is restarting.
Here is my docker compose file
version: "3.7"
services:
#Laravel App
app:
build:
args:
user: maruan
uid: 1000
context: ./docker/7.4
dockerfile: Dockerfile
# command: sh -c "start-container.sh"
image: laravel-app
restart: unless-stopped
working_dir: /var/www/
volumes:
- ./:/var/www
networks:
- app-network
#Nginx Service
nginx:
image: nginx:alpine
restart: unless-stopped
ports:
- 8000:80
volumes:
- ./:/var/www
- ./docker/7.4/nginx/conf.d:/etc/nginx/conf.d/default.conf
networks:
- app-network
#Mysl Service
db:
image: mysql:8
restart: unless-stopped
environment:
MYSQL_DATABASE: ${DB_DATABASE}
MYSQL_ROOT_PASSWORD: ${DB_PASSWORD}
MYSQL_PASSWORD: ${DB_PASSWORD}
MYSQL_USER: ${DB_USERNAME}
networks:
- app-network
networks:
app-network:
driver: bridge
Dockerfile
FROM php:7.4-fpm
# Arguments defined in docker-compose.yml
ARG user
ARG uid
WORKDIR /var/www
ENV TZ=UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# Install system dependencies
RUN apt-get update \
&& apt-get install -y --no-install-recommends build-essential mariadb-client libfreetype6-dev libjpeg-dev libpng-dev libwebp-dev zlib1g-dev libzip-dev gcc g++ make vim unzip git jpegoptim optipng pngquant gifsicle locales libonig-dev \
&& docker-php-ext-configure gd \
&& docker-php-ext-install gd \
&& apt-get install -y --no-install-recommends libgmp-dev \
&& docker-php-ext-install gmp \
&& docker-php-ext-install mysqli pdo_mysql zip \
&& docker-php-ext-enable opcache \
&& apt-get autoclean -y \
&& rm -rf /var/lib/apt/lists/* \
&& rm -rf /tmp/pear/
COPY . /var/www
# Get latest Composer
COPY --from=composer:latest /usr/bin/composer /usr/bin/composer
# Create system user to run Composer and Artisan Commands
RUN useradd -G www-data,root -u $uid -d /home/$user $user
RUN mkdir -p /home/$user/.composer && \
chown -R $user:$user /home/$user
COPY start-container.sh /usr/local/bin/start-container.sh
RUN chmod +x /usr/local/bin/start-container.sh
ENTRYPOINT ["start-container.sh"]
start-container.sh file
#!/usr/bin/env bash
set -e
cd /var/www
php artisan optimize
php artisan view:cache
#composer install && composer dump-autoload
exec "$#"
I also print log for that docker image.
Configuration cached successfully!
Route cache cleared!
Routes cached successfully!
Files cached successfully!
Compiled views cleared!
Blade templates cached successfully!
I think my error is docker container is restarting after running start-container.sh file. When I google, some people use PHP artisan script with ENTRYPOINT sh file.
What should I do not to restart again and again with ENTRYPOINT sh file?
Your entrypoint script ends with the line exec "$#". This runs the image's CMD, and is generally a best practice. However, your image doesn't have a CMD, so that command just expands to a bare exec, which causes the main container process to exit.
An image built FROM php:fpm often won't have a CMD line since the base image's Dockerfile specifies CMD ["php-fpm"]; it is enough to COPY your application code into a derived image, and the base image's CMD knows how to run it. However, setting ENTRYPOINT in a derived image resets the CMD from the base image (see the note in the Dockerfile documentation discussing CMD and ENTRYPOINT together). This means you need to repeat the base image's CMD:
ENTRYPOINT ["start-container.sh"]
CMD ["php-fpm"] # duplicated from base image, because you reset ENTRYPOINT

Laravel docker image "Could not open input file: /var/www/html/artisan"

I'm attempting to build and run a Laravel docker image which was originally generated via Laravel sail. When using docker-compose up, it correctly runs and I am able to access the site. Running docker build docker/7.4/ -t <tagname> followed by docker run <tagname> results in the following error logs however:
I have tracked this error down to my supervisord.conf file, where it is trying to call /var/www/html/artisan serve, but is seemingly unable to resolve it.
supervisord.conf:
[supervisord]
nodaemon=true
user=root
logfile=/var/log/supervisor/supervisord.log
pidfile=/var/run/supervisord.pid
[program:php]
command=/usr/bin/php -d variables_order=EGPCS /var/www/html/artisan serve --host=0.0.0.0 --port=80
user=sail
environment=LARAVEL_SAIL="1"
stdout_logfile=/dev/stdout
stdout_logfile_maxbytes=0
stderr_logfile=/dev/stderr
stderr_logfile_maxbytes=0
Here is also the Dockerfile I'm using, as well as the start-container script it runs
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 gosu curl zip unzip git supervisor sqlite3 libcap2-bin libpng-dev python2
RUN apt-get install -y gnupg ca-certificates
RUN mkdir -p ~/.gnupg \
&& chmod 600 ~/.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 php7.4-cli php7.4-dev \
php7.4-pgsql php7.4-sqlite3 php7.4-gd \
php7.4-curl php7.4-memcached \
php7.4-imap php7.4-mysql php7.4-mbstring \
php7.4-xml php7.4-zip php7.4-bcmath php7.4-soap \
php7.4-intl php7.4-readline php7.4-pcov \
php7.4-msgpack php7.4-igbinary php7.4-ldap \
php7.4-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 \
&& curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& echo "deb https://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& apt-get update \
&& apt-get install -y yarn \
&& apt-get install -y mysql-client \
&& apt-get install -y postgresql-client \
&& 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/php7.4
# RUN groupadd --force -g www-data sail
RUN useradd -ms /bin/bash --no-user-group -g www-data -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/7.4/cli/conf.d/99-sail.ini
RUN chmod +x /usr/local/bin/start-container
EXPOSE 8000
ENTRYPOINT ["start-container"]
start-container:
#!/usr/bin/env bash
if [ ! -z "$WWWUSER" ]; then
usermod -u $WWWUSER sail
fi
if [ ! -d /.composer ]; then
mkdir /.composer
fi
chmod -R ugo+rw /.composer
if [ $# -gt 0 ];then
exec gosu $WWWUSER "$#"
else
/usr/bin/supervisord -c /etc/supervisor/conf.d/supervisord.conf
fi
docker-compose.yml:
version: '3'
services:
laravel.test:
build:
context: ./docker/7.4
dockerfile: Dockerfile
args:
WWWGROUP: 'www-data'
container_name: laravel
image: sail-7.4/app
ports:
- '${APP_PORT:-80}:80'
environment:
WWWUSER: 'sail'
LARAVEL_SAIL: 1
volumes:
- '.:/var/www/html'
networks:
- sail
depends_on:
- mysql
mysql:
image: 'mysql:8.0'
container_name: 'mysql'
restart: 'unless-stopped'
tty: 'true'
ports:
- '${FORWARD_DB_PORT:-3306}:3306'
environment:
MYSQL_ROOT_PASSWORD: '${DB_PASSWORD}'
MYSQL_DATABASE: '${DB_DATABASE}'
MYSQL_USER: '${DB_USERNAME}'
MYSQL_PASSWORD: '${DB_PASSWORD}'
MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'
SERVICE_TAGS: 'dev'
SERVICE_NAME: 'mysql'
volumes:
- 'sailmysql:/var/lib/mysql'
networks:
- sail
healthcheck:
test: ["CMD", "mysqladmin", "ping"]
networks:
sail:
driver: bridge
volumes:
sailmysql:
driver: local
I am fairly new to using docker, so the process is somewhat confusing to me. Any help on where to look for this or how to debug it would be greatly appreciated - as well as an explanation as to why it's unable to resolve the php artisan command in supervisord.conf
Looking at your Dockerfile, you don't have a copy statement which copies the your source code to the container. And in your docker compose you would would be sharing that code using volumes. Hence your code works fine in docker-compose up. But in case of docker run <image> that volume is not shared and hence the error
simply add COPY . /var/www/html above COPY start-container /usr/local/bin/start-container

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 + Xdebug + VSCode Could not connect to client

PHP version: 7.1.20
XDebug version: 2.7.0
I'm using a Docker container on MacOS Mojave.
On pressing "F5", it shows debug options Pause, Restart, and Stop. But Step Over, Step Into, Step Out are disabled.
I'm kind of learner because I was used to code till 2012-13 on Windows OS. After that year, I am back to coding this month. :) Even after reviewing a lot of posts on Google about how to resolve this issue, I'm not sure how to finally make it work. Please help.
My launch.json file:
{
"version": "0.2.0",
"configurations": [
{
"name": "Listen for XDebug",
"type": "php",
"request": "launch",
"port": 9001,
"log": true,
"pathMappings": {
"/var/www/html": "${workspaceFolder}/learn"
}
},
{
"name": "Launch currently open script",
"type": "php",
"request": "launch",
"program": "${file}",
"cwd": "${fileDirname}",
"port": 9001
}
]
}
XDebug php.ini config:
xdebug.remote_host = 172.20.0.1
xdebug.remote_port = 9001
xdebug.scream = 1
xdebug.remote_enable = 1
xdebug.show_local_vars = 1
xdebug.remote_autostart = 1
xdebug.remote_connect_back = 1
xdebug.remote_log = "/var/www/html/xdebug.log"
xdebug.idekey = "VSCODE"
XDebug logfile (from setting xdebug.remote_log in php.ini):
[12] Log opened at 2019-05-12 10:16:44
[12] I: Checking remote connect back address.
[12] I: Checking header 'HTTP_X_FORWARDED_FOR'.
[12] I: Checking header 'REMOTE_ADDR'.
[12] I: Remote address found, connecting to 172.20.0.1:9001.
[12] W: Creating socket for '172.20.0.1:9001', poll success, but error: Operation now in progress (29).
[12] E: Could not connect to client. :-(
[12] Log closed at 2019-05-12 10:16:44
Debug console:
<- launchResponse
Response {
seq: 0,
type: 'response',
request_seq: 2,
command: 'launch',
success: true }
Here's the dockerfile.
FROM php:7.1.20-apache
RUN apt-get -y update --fix-missing
RUN apt-get upgrade -y
# Install tools & libraries
RUN apt-get -y install apt-utils nano wget dialog \
build-essential git curl libcurl3 libcurl3-dev zip
# Install important libraries
RUN apt-get -y install --fix-missing apt-utils build-essential git curl libcurl3 libcurl3-dev zip \
libmcrypt-dev libsqlite3-dev libsqlite3-0 mysql-client zlib1g-dev \
libicu-dev libfreetype6-dev libjpeg62-turbo-dev libpng-dev
# Composer
RUN curl -sS https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# PHP Extensions
RUN pecl install xdebug-2.7.0 \
&& docker-php-ext-enable xdebug \
&& docker-php-ext-install mcrypt \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install pdo_sqlite \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install curl \
&& docker-php-ext-install tokenizer \
&& docker-php-ext-install json \
&& docker-php-ext-install zip \
&& docker-php-ext-install -j$(nproc) intl \
&& docker-php-ext-install mbstring \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install -j$(nproc) gd \
&& pecl install redis \
&& docker-php-ext-enable redis
# Enable apache modules
RUN a2enmod rewrite headers
ENTRYPOINT ["/usr/sbin/apache2ctl", "-D", "FOREGROUND"]
Here's the docker-compose.yml file.
version: "3"
services:
webserver:
build:
context: ./bin/webserver
container_name: '7.1.x-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
mysql:
build: ./bin/mysql
container_name: '5.7-mysql'
restart: 'always'
ports:
- "3306:3306"
volumes:
- ${MYSQL_DATA_DIR-./data/mysql}:/var/lib/mysql
- ${MYSQL_LOG_DIR-./logs/mysql}:/var/log/mysql
environment:
MYSQL_ROOT_PASSWORD: tiger
phpmyadmin:
image: phpmyadmin/phpmyadmin
container_name: 'sc-phpmyadmin'
links:
- mysql
environment:
PMA_HOST: mysql
PMA_PORT: 3306
ports:
- '8080:80'
volumes:
- /sessions
redis:
container_name: 'sc-redis'
image: redis:latest
ports:
- "6379:6379"
Thank you in advance.
You can have your docker container connect to your host by configuring PHP appropriately.
Note that I've used Docker's host.docker.internal domain, that always points to the host IP.
xdebug.remote_enable=1
xdebug.remote_port=9000
xdebug.idekey=docker
xdebug.profiler_enable=0
xdebug.profiler_enable_trigger=1
xdebug.remote_host=host.docker.internal
There's no need to open ports to the docker container.
Just make sure that your debugging application can listen on the appointed port (9000 in my case) and that this port is not occupied.
Take for example this PhpStorm configuration:
Troubleshooting
Validate that xdebug is installed, run in the php container:
php -i | grep xdebug
Make sure that you're editing the appropriate .ini files, check:
php -i | grep \.ini
If you have idekey enabled, make sure to configure them in your IDE too:
There's a Debugger Config Validator in PhpStorm if you're exposing PHP scripts on a webserver:
Try to open port 9001 in the docker-compose.yml by inserting:
ports:
- "9001:9001"
Example
services:
webserver:
build:
context: ./bin/webserver
container_name: '7.1.x-webserver'
restart: 'always'
ports:
- "80:80"
- "443:443"
- "9001:9001"
links:
- mysql
volumes:
- ${DOCUMENT_ROOT-./www}:/var/www/html
- ${PHP_INI-./config/php/php.ini}:/usr/local/etc/php/php.ini
- ${VHOSTS_DIR-./config/vhosts}:/etc/apache2/sites-enabled
- ${LOG_DIR-./logs/apache2}:/var/log/apache2
See more about configuring docker-compose.yml: https://docs.docker.com/compose/compose-file/
You might be able to use expose. We have that in some of our old Dockerfiles which is intended to allow xDebug connections, but I haven't used it personally.
expose:
- "9001"
You should open port 9001 in your host machine, not in Docker.
Also try to check if Xdebug is configured properly: add phpinfo() to your code and open that page in browser. You shold see xdebug enabled
If Xdebug enabled, maybe your IDE settings are incorrect
For VSCode as the OP has asked and xdebug 3.x.x, the configuration should be -
xdebug.mode=debug
xdebug.start_with_request=yes
xdebug.start_upon_error=yes
xdebug.client_host=[IP address] <<<< NOTE
xdebug.discover_client_host=true
xdebug.client_port=9000
Note: xdebug.client_host should have the host IP address for vscode (192.168...) and not host.docker.internal which does not seem to work for vscode.
Note 2: There is no need to open any port (9000 in the config above) for this purpose. This could be because docker connects back to host at port 9000 and not vice versa!

Categories