DEV Community

Jens Kohl
Jens Kohl

Posted on

Run various PHP versions side-by-side via Docker using CodeRunner on macOS

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:

Setting up CodeRunner

  • First go to the settings (⌘;) → Languages and search for PHP in the list on the left.
  • Duplicate PHP with the »Settings«-Button below that list duplicate the PHP language
  • 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
Enter fullscreen mode Exit fullscreen mode

What this does is running a new container from the image named php:7.1. -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 /opt/project/$filename.
$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. Choose PHP 7.1 via Docker from Languages dropdown

Setting up Docker for Mac

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 Open Docker for Mac Preferences via its menu item
  • Go to the File Sharing tab and click the + button
  • 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
  • Enter /var/folders/pc and press return (↩) Docker for Mac after adding /var/folders/pc to the file sharing tab

Bringing it all together

You're no good to go, enter something like the script below to test your newly created CodeRunner docker environment:

echo 'PHP ' . phpversion() . PHP_EOL;

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

And after that you'll get the output of your PHP script: Running PHP 7.1 with CodeRunner via Docker

What to do next?

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.

Top comments (1)

leslieeeee profile image

Have you tried
It's a much easier tool for PHP developers, providing a user-friendly experience, especially for beginners. It supports all versions of PHP, MariaDB, PostgreSQL, as well as Redis and Memcached. You can run multiple PHP instances simultaneously and switch between them effortlessly. It also offers easy updates without the need to configure environment variables. This tool has greatly simplified my PHP development and is definitely worth trying!