Using Docker for running PHP Command Line Applications

A relatively complex command line application written in PHP might require Git, Composer and a few extensions to function properly. Do you know you can ship all those dependencies together to both local and production environment and ensure it is consistent across all platform?

Posted by Darwin Biler on February 24, 2017

What are PHP Command Line applications?

As the name suggests, its an application that is being run in terminal or command prompt. Usually to perform some CPU intensive background task that executes way too long to run directly via browser.

For example, applications built using the Symfony Console or Laravel Artisan frameworks.

These kind of real-world applications requires more than the php executable itself, but comes with requirements such as:

  • curl, json, mbstring, mcrypt, mysql and other php extensions
  • Composer to fetch the dependencies of the application
  • Git so that Composer can function properly
  • Basic editing tool such as vim
  • Basic mysql client
Although these tools might be readily available to your local workstation. In real world applications, you might need those basic toolset when deploying and troubleshooting the application in production servers.

Sometimes, due to high amount of data that needs to be processed, you might need to scale out the processing into multiple machines. Most of the time, those machines has "something else" also installed. Like a RoR application, a Memcached daemon or at worst cases, a legacy application that has very old PHP version installed, which makes installation of Composer for example, not possible.

Wouldnt it be great if there is a way to deploy your application into multiple environments and ensure all those tools and dependencies you need can be automatically installed in the target machines, while at the same time ensuring it wont conflict with the existing stuffs there?

Introducing Docker

In simple terms, you can think of Docker as an extremely lightweight virtual machine. Though its not exactly like that in lower level, but that is the idea - be able to run isolated processes in the same server w/o conflicting to each other.

You can get Docker in here and make sure you have docker and docker-compose commmands installed.

Once installed, you should create a docker-compose.yml file at the root of your project

and then, create another file called

You are now good to go! just hit up the following command to start fetching all the dependencies

docker-compose up -d

This will start fetching the base image php-production-cli which is basically a pre-built Docker Container that is specialized for running PHP command line applications.

This might take a while for the first time, but should be extremely fast the next time you do need to have another project which uses the same dependency.

Once everything is finish downloading. You can now execute your PHP command line application by

docker-compose exec cli bash

This will open a new terminal inside the container. The container has now everything you need to execute your php app! that is, ranging from Composer, Git, PHP extensions, vim, mysql clients etc.

That means, regardless of you are in a server, local PC, Mac or Windows. You'll only need two commands and voila, you have an instant environment for PHP application.

Did you find this useful?

I'm always happy to help! You can show your support and appreciation by Buying me a coffee (I love coffee!).