The previous article explained how to build a Laravel development environment using Docker. In fact, you can use Docker to run any PHP project on your local computer. To enable any PHP project to run in Docker environment is mainly required to make PHP image Dockerfile according to the dependencies of THE PHP project. This article will mainly talk about how to make PHP Docker image file according to the requirements.

1. Inherit the base image

First, you need to find the base image based on the PHP requirements of your PHP project, and then install the PHP extension based on the base image as needed. Use the FROM command in Dockerfile to specify the base image.

From <image>
Copy the code
  • FROMSpecifies the base source image to build the image. If there is no local image, it will automatically pull the image from the Docker public library.
  • FROMMust be the first directive in a non-comment line of a Dockerfile, i.e. a Dockerfile fromFROMStatement start.

Here we are going to make a PHP7.2 custom image that includes FPM, so first specify the base image in the Dockerfile:

The FROM PHP: 7.2 - FPMCopy the code

2. Install PHP extensions

The main job of customizing PHP images is to install various PHP extensions. There are three ways to install PHP image extensions in Dockerfile, each of which has its own specific usage scenarios, as described below.

2.1 Installing the official PHP extension in DockerHub

DockerHub provides some official PHP extensions, you can use docker-php-ext-install script command in Dockerfile list to install extensions, the following let us install PHP image on the common pDO, pdo_mysql, McRypt these extensions.

FROM PHP :7.2-fpm RUN docker-php-ext-install pdo pdo_mysql McRypt zip gd PCNTL opcache bcmathCopy the code

If you need to specify some custom build parameters for the extension, Docker provides another useful script, docker-php-ext-configure, to pass build parameters (usually not used).

FROM PHP :7.2-fpm RUN docker-php-ext-install pdo pdo_mysql McRypt zip PCNTL opcache bcmath\ && docker-php-ext-install -j$(nproc) iconv \ && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \ && docker-php-ext-install -j$(nproc) gdCopy the code

2.2 Installing the extension using PECL

DockerHub only provides a part of the PHP extension, install these extensions still need PECL, Docker image using PECL install download, compile, install extension and then use docker-php-ext-enable

The following uses PECL to install PHP redis and GRPC extensions.

RUN pecl install grpc \
     && docker-php-ext-enable grpc \
     && pecl install -o -f redis \
     && docker-php-ext-enable redis \
     && rm -rf /tmp/pear
Copy the code

2.3 Install the extension source code

Some extensions cannot be installed using docker-php-ext-install or PECL, but can only be installed from source code (PHP extensions like Phalcon do this, but more on this later). In Docker, PHP extensions can also be installed from source code

RUN curl -fsSL 'https://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz' -o xcache.tar.gz \
	&& mkdir -p /tmp/xcache \
	&& tar -xf xcache.tar.gz -C /tmp/xcache --strip-components=1 \
	&& rm xcache.tar.gz \
	&& docker-php-ext-configure /tmp/xcache --enable-xcache \
	&& docker-php-ext-install /tmp/xcache \
	&& rm -r /tmp/xcache
Copy the code

Write in the last

This article focuses on how to install PHP extensions in Docker. In fact, there are many other concepts related to Docker that need to be understood to make their own image files. Fortunately, there are detailed explanations in the official Docker tutorial, and those who are interested must follow the steps of the tutorial to practice themselves.

At present, the server-side development language is mainly used by Go in the service business organization, while PHP is mostly used by some old systems, management background or active systems. I can count the number of PHP codes I have written in the last two months, but I still spend more time writing Go codes. As a result, very few technical articles have been written about PHP classes recently, but I’ll write about installing and configuring Phalcon in Docker when I have time.