Using PHP 7 (or any other version) from the terminal using Docker
4th April, 2016
Docker has made it possible and easy to run specific software or applications in a precise, repeatable and sharable environment.
For example, lets see how to run our test suite from a specific version of PHP.
Firstly install Homebrew - the missing package manager for OS X.
Then install Docker $ brew install docker
(or download from the Docker toolbox)
Once Docker is installed you can run any image from the docker hub. We're interested in the official PHP image.
You can run a container with $ docker run php
But we want to supply some more arguments though:
$ docker run -it --rm -v "$PWD":/app -w /app php:7 php test.php
-i
(interactive) keep STDIN open if not attached
-t
allocate a pseudo TTY
--rm
automatically remove the container when it exits (good for space saving!)
-v "$PWD":/app
mount the current working directory (on your host) to the /app directory (on the docker
container).
-w /app
when the container launches, set the working directory to /app (in the container)
php:7
use the php image, version 7
php test.php
the command php
will execute the file test.php
(providing this exists in your local dir)
So if you are using phpspec and you want to test your specs work in PHP7 you could simply run
$ docker run -it --rm -v "$PWD:/app" -w /app php:7 php vendor/bin/phpspec r
To shorten the quite lengthy command above you could create an alias such as by executing
$ alias php7='docker run -it --rm -v "$PWD:/app" -w /app php:7 php'
or add the above to your ~/.bashrc file.
Then just use it with $ php7 vendor/bin/phpspec r
Further reading
Category: docker