/var/log

Using PHP from Docker in VSCode

When you run a PHP inside a Docker container, you can make VSCode uses the PHP from the Docker container. This can be done by creating an executable Bash script that dynamically gets the Docker container name and then runs the PHP binary from it.

Below an example of this Bash script. It reads the .env file that defined an APP_NAME variable. The name of the Docker container will have the name $APP_NAME-app. So if the APP_NAME is "MyTest", then the Docker container name will be "MyTest-app". Running PHP from this docker container can be done by executing docker exec MyTest-app php "$@".

#!/usr/bin/env bash

. ./.env

command="docker exec $APP_NAME-app php "$@""
$command

Save this file in your path, for example ~/bin/php-docker and make it executable.

Now you can enter the following settings in VSCode. Make sure to enter the PHP version you are using in Docker. Note that you can also set this in the workspace settings.

{
  "php.validate.executablePath": "/Users/peter/bin/php-docker",
  "intelephense.environment.phpVersion": "8.3.6"
}
Category: