[Vscode xdebug3.0 x PHP x Docker]

[Directory structure]

.docker-practice
├── html
│   └─ arraypractice.php //File to debug
├── php
│   ├── Dockerfile
│   └── php.ini
└── docker-compose.yml

[Final code and settings]

docker-compose.yml

version: '2'
services:
	php:
     build: ./php
     ports:
        - '80:80'
     volumes:
        - ./html:/var/www/html
        - ./php/php.ini:/usr/local/etc/php/php.ini

Dockerfile in the same php directory

FROM php:7.4-apache
# php.Ini is copied and installed in the docker container
COPY php.ini /usr/local/etc/php/
#zend required for xdebug install and configuration_Creating an extension
RUN pecl install xdebug \
  && docker-php-ext-enable xdebug
  # /usr/local/etc/php/conf.d/docker-php-ext-xdebug.to ini
  # zend_extension = ~Output

Added xdebug3.0 settings to php.ini

[xdebug]
xdebug.mode=debug
xdebug.start_with_request = yes
// host.docker.internal resolves the IP of docker's host machine.
xdebug.client_host=host.docker.internal
xdebug.client_port=9003

vscode launch.json

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Listen for XDebug",
            "type": "php",
            "request": "launch",
            "port": 9003,  // xdebug.client_Match to port
            "pathMappings": {
            //docker container side root directory:vscode root directory
                "/var/www/html": "${workspaceRoot}/html"
            }
        }
    ]
}

After that, if you add a breakpoint to the file directly under ./html and execute it & read it with a browser, it will stop at the breakpoint and variables will be displayed in VARIABLES. (On the 21st line, $ array after ksort is displayed properly.) 800A6CDE-D918-45F0-A97C-D189623072C2.jpeg

[How it became such a code]

1, At first I entered the docker container, and when I tried to install xdebug with pecl install xdebug, I got an error so that the php varsion was 7.2 or higher, so I changed the image of the Dockerfile from 7.0 to 7.4.

2, I wanted to make the xdebug item appear in phpinfo () for the time being, I added it because it was not displayed unless I made zend_extensin = ~ with docker-php-ext-enable xdebug. Also, since it is troublesome to reinstall xdebug every time I rebuild it, I made it possible to execute it with the RUN command of Dockerfile.

3, For php.ini, it didn't stop at breakpoint without xdebug.start_with_request = yes.

4, vscode launch.json, pathMappings doesn't make sense at first, It was like "./html": "{workspaceRoot}", but it was changed because it was found to be the root directory on the docker container side: vscode side workspace.

Recommended Posts

[Vscode xdebug3.0 x PHP x Docker]
Debug the VSCode + Docker + PHP development environment with XDebug.
Build Clang x VSCode on Docker (1)
Build Java x Spring x VSCode x Gradle on Docker (1)
Docker x Rails 6 (memo)
Php settings with Docker
Disposable PHP with Docker
WSL2 + VSCode + Docker development environment
[PDO → DB connection with docker, PHP]
Build PlantUML environment with VSCode + Docker