I'm doing a lot of small things in PHP these days, non of them is actual web development. The environments in which these scripts are meant to run have various PHP versions installed (I'm looking at you, CentOS LTS). Since most of my tasks involve short PHP scripts it isn't worth it to fire up phpStorm and setup a whole project.
So I went to my go-to tool for small scripts: CodeRunner, but right out of the box it only supports one version at a time for PHP, since it uses the pre-installed PHP which comes with macOS High Sierra fairly outdated. You can install other PHP versions via Homebrew (package manager) but using them in parallel isn't really a pleasure. I prefer using Docker for that. And that's what we gonna do, so let's start configuring CodeRunner:
- First go to the settings (
⌘;) → Languages and search for
PHPin the list on the left.
PHPwith the »Settings«-Button below that list
- Rename it to something meaningful, like
PHP 7.1 via Docker
- For the Run Command enter
docker run --rm -v $(pwd):/opt/project php:7.1 -f /opt/project/$filename
What this does is running a new container from the image named
-v $(pwd):/opt/project mounts the current directory in which CodeRunner will run your code into
/opt/project inside of the container.
-f /opt/project/$filename tells the php binary living inside of the container to run the script named
$filename is actually a placeholder for the filename CodeRunner will (temporarily) save your code to.
And last but not least,
--rm will ensure, that after CodeRunner executed your code, it will remove the just created container from your system again.
That has also the benefit, that every time you run your code within CodeRunner it gets executed in a fresh environment (everything you save from your code above the
/opt/project directory gets wiped)
- Close the settings window and bring the CodeRunner window to the front
- In the toolbar, there is a dropdown subtitled »Language«. Choose your newly created PHP environment labeled as
PHP 7.1 via Docker.
Before we can start, we have to give Docker for Mac permission to mount (the
-v flag) the directory where CodeRunner places its temporary files. To do that
- Open Docker for Mac »Preferences…« via its menu item
- Go to the
File Sharingtab and click the
- This will open the system open dialog. It doesn't really matter which directory you open, so you just click the »Open« button in the bottom right
- The newly create list item you have to double click. Now you can modify it
/var/folders/pcand press return (↩)
You're no good to go, enter something like the script below to test your newly created CodeRunner docker environment:
<?php echo 'PHP ' . phpversion() . PHP_EOL;
When you're using the
php:7.1 Docker image for the first time it needs to download it from the Docker Hub first, this can take some seconds to minutes, depending on your internet connection. It will look like the following:
Unable to find image 'php:7.1' locally 7.1: Pulling from library/php 85b1f47fba49: Already exists 66e22dddbf92: Already exists bf0df491fd2e: Already exists 0cbe7899c5b5: Already exists 515aeb1bd86c: Already exists 842bd485599e: Already exists Digest: sha256:9d847a120385a1181ffa8ba4d17f28968fb2285923a0ca690b169ee512c55cb1 Status: Downloaded newer image for php:7.1
Congratulations, you're done and can run your short PHP scripts via Docker in CodeRunner. You can clone the
PHP 7.1 via Docker environment for other PHP versions. Right now there are several versions in the official PHP docker image, even the latest pre-release versions of PHP 7.2.
But this approach is not limited to PHP. I guess you can setup something similar for your python, node.js and ruby environments.
But there's one caveat: You have to read the corresponding
Dockerfile to find out where to set the mount point (it's probably not
/opt/project) in the image, so that when running the docker container you find your files you want to execute.