VisualStudio Code PHP executablePath in docker

fcy6dtqo  于 2022-12-11  发布在  Docker
关注(0)|答案(5)|浏览(136)

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.

b4qexyjb

b4qexyjb1#

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.

egdjgwm8

egdjgwm82#

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": ""
}
velaa5lx

velaa5lx3#

If you are running Docker on Windows and WSL2 you should replace the underscores for dash like this:

path=$(printf '%s\n' "${PWD##*/}")
command="docker exec ${path}-laravel.test-1 php "$@""
echo "Running php on docker ${path}-laravel.test-1"
$command

It took me a while to find out, i hope this can help someone.

qyyhg6bp

qyyhg6bp4#

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

pftdvrlh

pftdvrlh5#

微软现在有一个扩展,允许你在容器上下文中打开一个vscode项目。这允许你使用在Docker容器中加载的PHP可执行文件来实现intellisense和其他功能。
https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.remote-containers
https://github.com/Microsoft/vscode-remote-release#readme

相关问题