Package it!

Now that I’m completely satisfied with the current development status of my application, I will build a Docker image (think of an image as a base class, as opposed to containers as specific object instances) so that I can:

Make a Sysadmin happy, and Let users who are afraid of PHP use my application without second thoughts.

The first step is to install Docker CE on my computer, and on all machines on which the application is expected to run. Having Docker CE installed allows both to build and run container-based applications.

Once that is done, building the image is rather simple. First, I drop a file named Dockerfile at the root of my project:

When choosing the image on which yours will be based, there’s a lot of possibilities. For PHP applications, I strongly suggest to use one of the official PHP images. In my case I chose the CLI version because I didn’t need Apache, and the Alpine distribution, which allows to have a lighter image.

In some cases (specifically when you don’t have any customization to do on the base image) you could completely skip the image building step and just use a bare PHP image to execute your application. In my case, the command to execute my application would look something like this (more on the arguments later):

docker run -it -rm --mount src="$(pwd)",target=/zero,type=bind --workdir /zero/ php:7.1-cli-alpine php ???? profit [1]

But in my case, it fails, because the PDO_MYSQL extension is not installed in the base image. Hence the second line of the Dockerfile. The docker-php-ext-install command is one of the scripts included in the official PHP images that help to customize your build, here by installing a PHP extension. You can also install additional packages by using the apk command provided by the Alpine base image.

The third line commits the project files in the image, so I don’t have to deploy/distribute the project files including the vendors via a separate channel. Finally, the fourth line defines the command that will be executed when one runs the container. The user will still be able to append arguments (such as the command name or options).