Skip to content

Instantly share code, notes, and snippets.

@a-l-e-c
Last active September 5, 2022 03:51
Show Gist options
  • Select an option

  • Save a-l-e-c/95751551ae8fa2f2df1412a3ce9901bb to your computer and use it in GitHub Desktop.

Select an option

Save a-l-e-c/95751551ae8fa2f2df1412a3ce9901bb to your computer and use it in GitHub Desktop.
Steps to setup VSCodium for SSH, xdebug & PHPUnit using PHP Interpreter within a Docker container

For anyone following something like Pascal Landau's tutorial, here are some adjustments for doing the same, but using VSCodium instead of PHPStorm as your IDE of choice

These steps will help you use the PHP Interpreter running inside a specific docker container (in these steps, the PHP I want to use is located in a docker container called dofroscra_local-php-fpm-1)

Requirements

  1. A single bash script we will use to run/debug the PHP code on our chosen docker container. For the instructions below the location of this bash script will be shown as /path/to/bash/script/php1
#!/bin/bash
container=$PHP_DOCKER_CONTAINER
if [ -z "$container" ]
then
  container=$1
  shift
fi
docker exec $container php $@
  1. An entry to a .env file (in your existing project) to reference the name of the docker container. Instructions below are using the existing /.make/.env
PHP_DOCKER_CONTAINER=dofroscra_local-php-fpm-1

NOTE: Use Workspace settings in VSCodium.

Step 1 (configure location of PHP Interpreter)

Add the executablePath settings to access PHP via our php bash script:

"php.validate.executablePath": "/path/to/bash/script/php",
"php.debug.executablePath": "/path/to/bash/script/php",

Step 2 (configure to run PHP, debug with Xdebug & test with PHPUnit)

Using the PHP Debug extension, add this to the Workspace settings:

"launch": {
		"version": "0.2.0",
		"configurations": [
			{
				"name": "Run currently open script",
				"type": "php",
				"request": "launch",
				"runtimeArgs": ["${env:PHP_DOCKER_CONTAINER} ${relativeFile}"],
				"envFile": "${workspaceFolder}/.make/.env"
			},
			{
				"name": "Debug currently open script",
				"type": "php",
				"request": "launch",
				"port": 9003,
				"runtimeArgs": ["${env:PHP_DOCKER_CONTAINER} ${relativeFile}"],
				"pathMappings": {
					"/var/www/app": "${workspaceFolder}",
				},
				"envFile": "${workspaceFolder}/.make/.env",
			},
			{
				"name": "Listen for Xdebug",
				"type": "php",
				"request": "launch",
				"port": 9003,
				"pathMappings": {
					"/var/www/app": "${workspaceFolder}",
				},
				"envFile": "${workspaceFolder}/.make/.env"
			},
			{
				"name": "Run PHPUnit tests",
				"type": "php",
				"request": "launch",
				"runtimeArgs": [
					"vendor/bin/phpunit --configuration phpunit.xml"
				],
				"envFile": "${workspaceFolder}/.make/.env"
			}
		]
	}

[OPTIONAL] Configure SSH access

To access the PHP Interpreter we won't be needing ssh, but I've added the settings in the event it's needed. Using the sshfs extension, add the following in your Workspace settings:

"settings": {
  "sshfs.configs": [{
    "root": "/var/www/app",
    "host": "localhost",
    "port": 2222,
    "username": "application",
    "password": "123456",
    "name": "docker-php-tutorial_ssh"
  }],
  //settings from step 1 go here
},
//settings from step 2 go here


Complete example of basic Workspace settings in VSCodium:

{
	"folders": [
		{
			"path": "docker-php-tutorial"
		}
	],
	"settings": {
		"sshfs.configs": [
			{
				"root": "/var/www/app",
				"host": "localhost",
				"port": 2222,
				"username": "application",
				"password": "123456",
				"name": "docker-php-tutorial_ssh"
			}
		],
		"php.validate.executablePath": "/path/to/bash/script/php",
		"php.debug.executablePath": "/path/to/bash/script/php",
	},
	"launch": {
		"version": "0.2.0",
		"configurations": [
			{
				"name": "Run currently open script",
				"type": "php",
				"request": "launch",
				"runtimeArgs": ["${env:PHP_DOCKER_CONTAINER} ${relativeFile}"],
				"envFile": "${workspaceFolder}/.make/.env"
			},
			{
				"name": "Debug currently open script",
				"type": "php",
				"request": "launch",
				"port": 9003,
				"runtimeArgs": ["${env:PHP_DOCKER_CONTAINER} ${relativeFile}"],
				"pathMappings": {
					"/var/www/app": "${workspaceFolder}",
				},
				"envFile": "${workspaceFolder}/.make/.env",
			},
			{
				"name": "Listen for Xdebug",
				"type": "php",
				"request": "launch",
				"port": 9003,
				"pathMappings": {
					"/var/www/app": "${workspaceFolder}",
				},
				"envFile": "${workspaceFolder}/.make/.env"
			},
			{
				"name": "Run PHPUnit tests",
				"type": "php",
				"request": "launch",
				"runtimeArgs": [
					"vendor/bin/phpunit --configuration phpunit.xml"
				],
				"envFile": "${workspaceFolder}/.make/.env"
			}
		]
	}
}

Footnotes

  1. The script will fall back to using the first parameter passed as the container name. So from the terminal you could do something like ~/location/php container_name -v"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment