From 39e99e6a094853f8deefeed95fb5d01d5f93b4cc Mon Sep 17 00:00:00 2001 From: Michael Stolovitzsky Date: Thu, 6 Mar 2025 15:43:20 +0000 Subject: [PATCH] vscode devcontainer support via xdebug - Make sure PHP debugger and devcontainer extensions are installed - Install docker on your system - In .vscode, copy `launch.json-devcontainer-dist` to `launch.json` or use its contents for an existing launch.json file - Build the devcontainer and switch the workspace to devcontainer When you run the debug server launch configuration, a PHP webserver inside the devcontainer will start and you will be able to use the vscode debugger: place breakpoints, use watches, catch exceptions, step over etc. Note that xdebug server runs on your machine and listens on its port 9003. The xdebug client is a php extension that runs _in_ the devcontainer and will connect back to your host, which is why vscode should have port 9003 listed in forwarded ports and it should be green when the web server is running. --- .devcontainer/Dockerfile | 15 +++++++++++ .devcontainer/devcontainer.json | 28 +++++++++++++++++++++ .devcontainer/docker-compose.yml | 36 +++++++++++++++++++++++++++ .devcontainer/postinstall.sh | 6 +++++ .devcontainer/xdebug.ini | 7 ++++++ .vscode/.gitignore | 1 + .vscode/launch.json-devcontainer-dist | 28 +++++++++++++++++++++ 7 files changed, 121 insertions(+) create mode 100644 .devcontainer/Dockerfile create mode 100644 .devcontainer/devcontainer.json create mode 100644 .devcontainer/docker-compose.yml create mode 100755 .devcontainer/postinstall.sh create mode 100644 .devcontainer/xdebug.ini create mode 100644 .vscode/.gitignore create mode 100644 .vscode/launch.json-devcontainer-dist diff --git a/.devcontainer/Dockerfile b/.devcontainer/Dockerfile new file mode 100644 index 00000000000..5e7d963ce00 --- /dev/null +++ b/.devcontainer/Dockerfile @@ -0,0 +1,15 @@ +FROM mcr.microsoft.com/devcontainers/php:1-8.3-bookworm + +# Install MariaDB client and network debug tools +RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \ + && apt-get install -y mariadb-client libpng-dev libicu-dev libzip-dev inetutils-ping netcat-openbsd tcpdump\ + && apt-get clean -y && rm -rf /var/lib/apt/lists/* + +# Install necessary PHP extensions +RUN docker-php-ext-install mysqli pdo pdo_mysql gd intl zip + +# [Optional] Uncomment this section to install additional OS packages. + +# [Optional] Uncomment this line to install global node packages. +# RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g " 2>&1 + diff --git a/.devcontainer/devcontainer.json b/.devcontainer/devcontainer.json new file mode 100644 index 00000000000..29800f95cf3 --- /dev/null +++ b/.devcontainer/devcontainer.json @@ -0,0 +1,28 @@ +// For format details, see https://aka.ms/devcontainer.json. For config options, see the +// README at: https://github.com/devcontainers/templates/tree/main/src/php-mariadb +{ + "name": "Webtrees PHP", + "dockerComposeFile": "docker-compose.yml", + "service": "app", + "workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}", + + // Features to add to the dev container. More info: https://containers.dev/features. + "features": { + "ghcr.io/opencodeco/devcontainers/install-php-extensions:latest": {}, + "ghcr.io/devcontainers-extra/features/composer:1": {} + }, + + "forwardPorts": [ + 8080, + 3306 + ], + + // Use 'postCreateCommand' to run commands after the container is created. + "postCreateCommand": ".devcontainer/postinstall.sh" + + // Configure tool-specific properties. + // "customizations": {}, + + // Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root. + // "remoteUser": "root" +} diff --git a/.devcontainer/docker-compose.yml b/.devcontainer/docker-compose.yml new file mode 100644 index 00000000000..70224a6afe8 --- /dev/null +++ b/.devcontainer/docker-compose.yml @@ -0,0 +1,36 @@ +version: '3.8' + +services: + app: + build: + context: . + dockerfile: Dockerfile + + volumes: + - ../..:/workspaces:cached + + # Overrides default command so things don't shut down after the process ends. + command: sleep infinity + + + + # Use "forwardPorts" in **devcontainer.json** to forward an app port locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + + db: + image: mariadb:10.4 + restart: unless-stopped + volumes: + - mariadb-data:/var/lib/mysql + environment: + MYSQL_ROOT_PASSWORD: mariadb + MYSQL_DATABASE: mariadb + MYSQL_USER: mariadb + MYSQL_PASSWORD: mariadb + + # Add "forwardPorts": ["3306"] to **devcontainer.json** to forward MariaDB locally. + # (Adding the "ports" property to this file will not forward from a Codespace.) + +volumes: + mariadb-data: + diff --git a/.devcontainer/postinstall.sh b/.devcontainer/postinstall.sh new file mode 100755 index 00000000000..5f82873f0ea --- /dev/null +++ b/.devcontainer/postinstall.sh @@ -0,0 +1,6 @@ +sudo cp .devcontainer/xdebug.ini /usr/local/etc/php/conf.d/xdebug.ini +composer update +composer install +sudo chmod a+x $(pwd) +sudo rm -rf /var/www/html +sudo ln -s $(pwd) /var/www/html diff --git a/.devcontainer/xdebug.ini b/.devcontainer/xdebug.ini new file mode 100644 index 00000000000..79e11fd1a2f --- /dev/null +++ b/.devcontainer/xdebug.ini @@ -0,0 +1,7 @@ +[xdebug] +zend_extension=xdebug +xdebug.mode=develop,debug +xdebug.start_with_request = yes +xdebug.client_port = "9003" +xdebug.idekey="vsc" +xdebug.log=/tmp/xdebug_remote.log \ No newline at end of file diff --git a/.vscode/.gitignore b/.vscode/.gitignore new file mode 100644 index 00000000000..450a23404be --- /dev/null +++ b/.vscode/.gitignore @@ -0,0 +1 @@ +launch.json diff --git a/.vscode/launch.json-devcontainer-dist b/.vscode/launch.json-devcontainer-dist new file mode 100644 index 00000000000..d45bc0bddc6 --- /dev/null +++ b/.vscode/launch.json-devcontainer-dist @@ -0,0 +1,28 @@ +{ + // 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": "Debug server", + "type": "php", + "request": "launch", + "runtimeArgs": [ + "-dxdebug.mode=debug", + "-dxdebug.start_with_request=yes", + "-S", + "0.0.0.0:8080" + ], + "program": "", + "cwd": "${workspaceRoot}", + /* This is the debugger port on your dev machine that the container will connect to, xdebug.ini uses it*/ + "port": 9003, + "serverReadyAction": { + "pattern": "Development Server \\(http://\w:([0-9]+)\\) started", + "uriFormat": "http://localhost:%s", + "action": "openExternally" + } + } + ] +} \ No newline at end of file