ide vscode

This guide explains how to configure Visual Studio Code (VSCode) to use PHP inside a Docker container as the executable path. Reference: How to setup VSCode with PHP inside docker

Environment

  • Ubuntu 22.04.3 LTS (running on WSL)
  • Docker Engine 26.0.0
  • PHP 8.2.15
  • Visual Studio Code 1.87.2

Prerequisites

  • You have a Docker container that has PHP installed. For instructions on setting up a Laravel project running on Docker in WSL, see this guide.
  • Visual Studio Code is installed.

Setup Steps

  1. Create a Script to Run PHP Inside the Docker Container
  2. Update the PHP Settings in VSCode

1. Create a Script to Run PHP Inside the Docker Container

First, create a script that allows you to run PHP inside your Docker container.

If Using Docker Compose

Create a file named php.sh in the same directory where your docker-compose.yml is located within your project.

php.sh:

#!/bin/bash

docker compose exec <service-name> php $@

If Not Using Docker Compose

Create a php.sh file in any directory within your project.

php.sh :

#!/bin/bash

docker exec -it <container-name> php $@

$@ represents all the arguments passed to the shell script.
For example, running php.sh <arg1> <arg2> will forward <arg1> <arg2> to the php command inside the container. This allows you to run PHP inside Docker with any arguments.

2. Update the PHP Settings in VSCode

Now update VSCode the PHP Settings in VSCode.

Open VSCode and click on Settings.

Settings Button

Switch to the “Workspace” tab, go to “Extensions” → “PHP”, and click “Edit in settings.json” in php.validate.executablePath.

Edit in settings.json link

Add the path to php.sh to the php.validate.executablePath field.
In this example, the script is placed in the root directory of the workspace.

setting.json :

{
    "php.validate.executablePath": "./php.sh", // Added
    "php.validate.run": "onType"
}

Setting “php.validate.run” to “onType” enables real-time validation of your PHP code as you type, displaying errors immediately.

Now your PHP executable path in VSCode points to PHP inside the Docker container.
You should also stop seeing warnings related to the PHP executable path.