PHP development with Docker

I’m the kind of dev that dreads configuring webservers and that rather does not have to put up with random ops stuff before being able to get work done. Docker is one of those things I’ve never looked into, cause clearly it’s evil annoying boring evil confusing evil ops stuff. Two of my colleagues just introduced me to a one-line docker command that kind off blew my mind.

Want to run tests for a project but don’t have PHP7 installed? Want to execute a custom Composer script that runs both these tests and the linters without having Composer installed? Don’t want to execute code you are not that familiar with on your machine that contains your private keys, etc? Assuming you have Docker installed, this command is all you need:

This command uses the Composer Docker image, as indicated by the first composer at the end of the command. After that you can specify whatever you want to execute, in this case composer ci, where ci is a custom composer Script. (If you want to know what the Docker image is doing behind the scenes, check its entry point file.)

This works without having PHP or Composer installed, and is very fast after the initial dependencies have been pulled. And each time you execute the command, the environment is destroyed, avoiding state leakage. You can create a composer alias in your .bash_aliases as follows, and then execute composer on your host just as you would do if it was actually installed (and running) there.

Of course you are not limited to running Composer commands, you can also invoke PHPUnit

or indeed any PHP code.

This one liner is not sufficient if you require additional dependencies, such as PHP extensions, databases or webservers. In those cases you probably want to create your own Docker file. Though to run the tests of most PHP libraries you should be good. I’ve now uninstalled my local Composer and PHP.

To get started, install Docker and add your user to the docker group (system restart might be needed afterwards):

1 thought on “PHP development with Docker”

  1. So, I get that docker is nice, but to uninstall php and composer? I practice TDD which means right inside phpstorm I am constantly writing a little bit of test, then some code, then executing the test, then repeat. Which means PhpStorm needs to know about php and phpunit (which is brought in by composer) I see how you could create an alias to run your entire test suite but often when I’m doing this I’m just executing one test case for the class I’m creating at the time.

    I’m rather new to docker though and am still trying to figure out how to best use it for development. There are many articles about deploying with it, but to develop with it? The work flow seems a little odd…

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.