Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 572
Stillmatic1985 Avatar asked Nov 27 '18 14:11

Stillmatic1985


3 Answers

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.

like image 141
Steferson Avatar answered Oct 17 '22 02:10

Steferson


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": ""
}
like image 7
FDisk Avatar answered Oct 17 '22 04:10

FDisk


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.

like image 2
Douglas Lopes Avatar answered Oct 17 '22 02:10

Douglas Lopes