'VisualStudio Code PHP executablePath in docker
I try to configure VSCode to use our php executable inside a docker container. Firstly i tried it on a macintosh and everything works as expected. At work we use windows pc´s and i cant get it to work.
Workspace Settings
"php.suggest.basic": false,
"php.executablePath": "C:\\Source\\stack\\.bin\\php.bat",
"php.validate.executablePath": "C:\\Source\\stack\\.bin\\php.bat",
"php.validate.run": "onSave",
"php.validate.enable": true
I tried to set a .sh, .exe or .bat file but none of them seemed to work.
php.bat
@echo off
docker run -i stack_php php %*
php.sh
#!/bin/sh
docker run stack_php php "$@"
return $?
Anybody of you can help me get this to work? We would like to change our IDE from PHPStorm to VSCode but we arent able to so because everything a developer needs is stored in docker containers.
Solution 1:[1]
I came up with a solution on Linux for multiple laravel sail projects.
Create a file named 'php' on /usr/local/bin
sudo touch /usr/local/bin/php
Make it executable:
sudo chmod +x /usr/local/bin/php
Edit the file (with sudo) and paste this code:
path=$(printf '%s\n' "${PWD##*/}")
command="docker exec ${path}_laravel.test_1 php "$@""
echo "Running php on docker ${path}_laravel.test_1"
$command
Now just run, example, 'php -v' inside the laravel sail project.
Solution 2:[2]
Or you can use plugin for that: https://marketplace.visualstudio.com/items?itemName=henriiik.docker-linter
Example how to configure it correctly could be found in their repository https://github.com/henriiik/vscode-docker-linter/blob/master/playground-php/.vscode/settings.json
You will need to modify the existing settings to look like this example below...
{
"php.validate.enable": false,
"docker-linter.php.enable": true,
"docker-linter.php.container": "stack_php",
"docker-linter.php.machine": ""
}
Solution 3:[3]
I had the exact same issue, and just found a solution, but in my case I'm working under the Windows Subsystem for Linux, not straight Windows, so I'm not sure if/how it would be adapted. You can read more about it here: set PHP path from host to docker container
For me, the part that seemed to resolve the problem was moving the "wrapper" to /usr/local/bin, and then setting that as my php.validate.executablePath for the Workspace (you could also set it at the Remote WSL level, but in my case, I might develop with multiple versions of PHP)
I hope that might help you!
Update: I just saw someone in the comments was working in Windows! They suggested putting
docker exec -i your_container_name php %*
into a .bat file; looking at what you've already done, the difference appears to be using exec instead of run
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Steferson |
| Solution 2 | |
| Solution 3 | Malcalevak |
