Running dockerized executables from Atom/VS Code editor

Text below originally was written for Atom, but nowadays I’ve been using VS Code and the same solution can apply for that editor too.

Atom is a fairly nice editor. It’s slower at times than my favorite Vim, but it has more power when extended with various packages. I’ve started using Atom to work on a new PHP project and with it I’ve installed such packages as: linter-php, php-fmt and atom-autocomplete-php. These there packages make sure that I don’t have any typos in my code, let me peek into Class and Method definitions, do smart autocomplete and finally automatically format PHP in a PSR-2 coding style.

That’s all nice, however each of these packages require PHP to be installed in the system in order to run variousphp commands — I’m not a fan of that. I’ve been using Docker to run dozen of containers that keep my development environments for over two years now. I really don’t want to turn my OS (I run Ubuntu) into a host for PHP, Python, C++, Node.js, Go and Rust executables and compilers. So, how do we run PHP scripts from within the Atom even though PHP is not installed on the system?

Surprisingly, it is simpler than it might seem. Initially I was hoping that in the settings for each package I could specify command docker exec -i php7 php as a path to my executable. Unfortunately that didn’t work out due to the way Atom tries to execute given command (from my understanding — whole line as a single path to executable file).

We have another way around — by default each of the packages try to access php executable which in case of Linux might be located in number of folders:

Since command to the docker didn’t work out maybe instead we could trick Atom into believing that one of those folder does have required executable and proxy everything into the Docker? And indeed that’s the how it could be done.

All that is needed in order to run Dockerized PHP executables from within the Atom is to create an executable file in one of the folders listed above. I decided to go with /usr/local/bin . In this folder create file php and set its permissions to executable via sudo chmod +x php . Open that file and add following lines:

docker exec -i --user=1000:1000 {PHP Docker container name} php "$@"

First line tells us that this file should be executed as a bash script. In second line we ask docker to run php command within a specified docker container and passes all arguments that were given to the script. Essentially we’ve created a proxy executable file that internally executes PHP within the docker container and returns output to the Atom that is running on the parent OS. Note --user=1000:1000 this option tells docker to execute PHP with same permissions as my parent OS user. It is needed in order to prevent PHP from being run as a root user which in case of php-fmt could otherwise override your files with a Docker user in turn preventing you from editing those files until you restore permissions.

In my case this file would look like:

docker exec -i --user=1000:1000 docker_php7 php "$@"

Save the file and that’s it — Atom would be able to run php command. But not just Atom — you might find that you too can run php in your terminal.

This approach should work for other executables — I use it in order to run Composer executable too.