How to setup VSCode with PHP inside docker

I Love VSCode and I am sure you do too which is probably the reason why you are reading this. I have been an avid user of VSCode for several years now and I love the flexibility it provides with third party extensions and plugins.

I have to wirte a lot of PHP code for a bunch of different projects and I like to use docker for local development environment. Docker makes it very easy to emulate production environments on your local machine which is the primary reason I have ditched VirtualBox completely for the past couple of years and switched to docker completely.

After moving to docker I could not find a proper way to add the PHP support to VSCode as it requires an executablePath to the php executable to work.

To overcome this issue all you need to do is to create a file in you project directory by the name of `php` and add the following code to it

#!/bin/bash
docker exec -it web_app php $@

`web_app` is the name of the container that has PHP installled in it. Now we need to make this file executable if you are on linux or mac using this command `chmod +x /path/to/project/php`.

Now you can update the `executablePath` and set its value to this new file that you have just created

"php.validate.executablePath": "./php"

Now you should have the PHP support in VSCode without installing PHP on your local machine.

If you know of a better way to do this, Please share in comments.

Leave a Reply