How to use PHP inside a Docker Container using an IDE like VS Code (without installing PHP locally)

Filipe Pires
Nerd For Tech
Published in
4 min readApr 18, 2024

Many PHP developers opt for Docker-exclusive environments on their machines, yet often grapple with configuring essential PHP plugins in IDEs like Visual Studio Code. While some maintain local PHP installations for CLI operations or IDE plugins like IntelliSense/Intelephense, relying solely on Docker-hosted PHP can streamline workflows and conserve system resources like CPU and RAM.

Consider the dilemma of differing PHP versions: perhaps you have PHP 8.3 locally but your Docker container runs PHP 8.0. This mismatch can trigger errors and render certain operations or package requirements unsupported, disrupting your development process.

This solution proves invaluable when managing multiple PHP projects that require different PHP versions. Whether it’s maintaining PHP 7 for legacy web applications, adopting PHP 8.3 for the latest Laravel version, or accommodating projects targeted at PHP 8.1, this method ensures seamless development across diverse PHP environments.

Disclaimer: While experienced developers may execute PHP commands within Docker containers, IDEs often lack native support for this functionality. Constantly adjusting settings to align with the container’s PHP executable path can be cumbersome and disrupt workflow efficiency, which I find less than ideal.

Warning: this is only for the developers who don't want to have PHP installed locally on the machine.

Notice: This only was tested on Mac OSX and Linux, Windows I didn't have the opportunity to test it.

So I created this solution, that works by specifying the container names or container patterns in a custom script locally that enables running the PHP version installed on the container, so when running on an IDE, like Visual Studio Code, PHP is available from the command line without configuring their settings because it’s available system-wide.

The primary objective is to redirect the operating system to locate the PHP executable within the accessible Docker container rather than relying on a local installation. We gonna trick the OS. 🙈

Here are the steps you must follow:

1 — Open your terminal

2 — Create an empty file named "php" on this location "/usr/local/bin":
sudo touch /usr/local/bin/php

3 — Let’s edit the file and paste the script, where is an example using NANO:
sudo nano /usr/local/bin/php


#!/bin/bash

# Define container names and pattern
containerNames=("your_webcontainer1_name" "your_webcontainer2_name")
secondaryPattern=".*-laravel\.test-1$"

# Initialize container name variable
containerName=""

# Iterate over container names array
for name in "${containerNames[@]}"; do
# Check if the container is up
containerName=$(docker ps --format "{{.Names}}" | grep -E "\b$name\b")
if [ -n "$containerName" ]; then
break # Exit loop if a container is found
fi
done

# If no container is found, find a container that matches the secondary pattern
if [ -z "$containerName" ]; then
containerName=$(docker ps --filter "name=$secondaryPattern" --format "{{.Na$
fi

# Check if containerName is empty
if [ -z "$containerName" ]; then
echo "Error: Could not find any of the specified containers or any containe$
exit 1
fi

echo "Container name: $containerName"

# Modify the command to use the correct container name
command="docker exec $containerName php "$@""
echo "Running php on Docker container: $containerName"

# Execute the command
$command

Note: Please ensure to customize the variables containerNames and secondaryPattern to match the names of your Docker containers.

The containerNames variable is an array capable of holding multiple container names. If you're working with a Laravel application using Sail, the secondaryPattern demonstrates the pattern required. If you're not using patterns or names, leave the variables empty, but ensure that at least one is filled to avoid errors.

4 — After editing the variables to your container names/patterns, let's save the file, in the NANO editor, we use: CTRL+O to save and thenCTRL+X to exit.

5 — Now we will make the file executable using: sudo chmod +x /usr/local/bin/php

6 — Make sure you have a docker container with php running which the name is on the script, and the only thing you need to check if all is good, just type in the IDE integrated terminal or any terminal: php -v

It will return your docker container PHP version, so you can now be capable of running PHP commands from the terminal using only php command.

note: If you need to switch projects, don’t forget to put down the previous running container so that the script only catches active containers. The script only supports one PHP container running, if you have multiple it will pick from the order from the docker ps command

I hope this script/method will help someone who needs this problem solved, and as always: 👏 if you like, or comment if you need some extra clarification.

--

--

Filipe Pires
Nerd For Tech

Dad, Senior Software Engineer, Game Dev & Collector, Electronic Prototyping & much more. Passionate about building the future with code.