Docker php installation extension steps

Keywords: PHP Docker Container

Demonstrate what the four commands docker PHP source, docker PHP ext install, docker PHP enable docker configure are used for and what they do in the PHP container?

The PHP container in Docker installs extensions in the following ways:

  1. Installation by pecl
  2. It is installed through several special commands in the php container, which can be used in the RUN command in Dockerfile.

Here, we mainly discuss the second scheme, how to install PHP extensions through several special commands in the PHP container

There are several special commands for installing extensions in PHP

  • docker-php-source
  • docker-php-ext-install
  • docker-php-ext-enable
  • docker-php-ext-configure

Demonstrate the role of these three commands

They are all demonstrated in the PHP container. The PHP container startup is too simple to be introduced

docker-php-source

This command actually creates a directory of / usr/src/php in the PHP container, which contains some of its own files. We just take it as a storage directory of PHP extension source code downloaded from the Internet. In fact, all PHP extension source code extensions are stored in the path: / usr/src/php/ext.

Format:

docker-php-source extract | delete

Parameter Description:
*extract: create and initialize the / usr/src/php directory
*Delete: delete the / usr/src/php directory

Case:

root@803cbcf702a4:/usr/src# ls -l
total 11896 #At this point, there is no php directory
-rw-r--r-- 1 root root 12176404 Jun 28 03:23 php.tar.xz
-rw-r--r-- 1 root root      801 Jun 28 03:23 php.tar.xz.asc

root@803cbcf702a4:/usr/src# docker-php-source extract
root@803cbcf702a4:/usr/src# ls -l
total 11900 #At this time, the php directory is produced, and there are still some files in it. Due to the length problem, I won't go in and check it
drwxr-xr-x 14 root root     4096 Aug  9 09:01 php
-rw-r--r--  1 root root 12176404 Jun 28 03:23 php.tar.xz
-rw-r--r--  1 root root      801 Jun 28 03:23 php.tar.xz.asc

root@803cbcf702a4:/usr/src# docker-php-source delete
root@803cbcf702a4:/usr/src# ls -l
total 11896 #At this point, the created php directory is deleted
-rw-r--r-- 1 root root 12176404 Jun 28 03:23 php.tar.xz
-rw-r--r-- 1 root root      801 Jun 28 03:23 php.tar.xz.asc

root@803cbcf702a4:/usr/src#

docker-php-ext-enable

This command is used to start   PHP extension   of When we use pecl to install the PHP extension, the extension is not started by default. If you want to use this extension, you must configure it in the php.ini configuration file to use this PHP extension. The docker PHP ext enable command is automatically given to us to start the PHP extension. You don't need to go to php.ini Mo Xiaosong Configure in the configuration file.
case

# View existing extensions that can be started
root@517b9c67507a:/usr/local/etc/php# ls /usr/local/lib/php/extensions/no-debug-non-zts-20170718/
opcache.so  redis.so  sodium.so
root@517b9c67507a:/usr/local/etc/php#

# Check whether the redis extension can be started
root@517b9c67507a:/usr/local/etc/php# php -m | grep redis
root@517b9c67507a:/usr/local/etc/php#

# Start redis extension
root@517b9c67507a:/usr/local/etc/php# docker-php-ext-enable redis
# Start successful
root@517b9c67507a:/usr/local/etc/php# php -m | grep redis
redis
root@517b9c67507a:/usr/local/etc/php#

#Note: there is no php.ini configuration file in the PHP container by default. The loading principle is as follows

root@517b9c67507a:/usr/local/etc/php# php -i | grep -A 5 php.ini
Configuration File (php.ini) Path => /usr/local/etc/php
Loaded Configuration File => (none)
# The core is the extended configuration file in the / usr/local/etc/php/conf.d directory
Scan this dir for additional .ini files => /usr/local/etc/php/conf.d
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-redis.ini,
/usr/local/etc/php/conf.d/docker-php-ext-sodium.ini

root@517b9c67507a:/usr/local/etc/php#

docker-php-ext-install

This command is used to install and start PHP extensions.
Command format:
Docker PHP ext install "directory name of source package"

Note:

  • The "source package" needs to be placed under / usr/src/php/ext
  • By default, the PHP container does not have the directory / usr/src/php, which needs to be generated using docker PHP source extract.
  • After the installation of docker PHP ext install installed extensions, docker PHP ext enable will be automatically called to start the installed extensions.
  • Uninstall the extension and directly delete the configuration file corresponding to / usr/local/etc/php/conf.d.

case

# Uninstall redis extension
root@803cbcf702a4:/usr/local# rm -rf /usr/local/etc/php/conf.d/docker-php-ext-redis.ini
root@803cbcf702a4:/usr/local# php -m 
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
ftp
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

root@803cbcf702a4:/usr/local#

#The PHP container does not have the redis extension by default, so we install the redis extension through docker PHP ext install

root@803cbcf702a4:/# curl -L -o /tmp/reids.tar.gz https://codeload.github.com/phpredis/phpredis/tar.gz/5.0.2

root@803cbcf702a4:/# cd /tmp
root@517b9c67507a:/tmp# tar -xzf reids.tar.gz
root@517b9c67507a:/tmp# ls
phpredis-5.0.2  reids.tar.gz
root@517b9c67507a:/tmp# docker-php-source extract
root@517b9c67507a:/tmp# mv phpredis-5.0.2 /usr/src/php/ext/phpredis

#Check whether the plug-in source code package moved in the past exists
root@517b9c67507a:/tmp# ls -l /usr/src/php/ext | grep redis
drwxrwxr-x  6 root root 4096 Jul 29 15:04 phpredis
root@517b9c67507a:/tmp# docker-php-ext-install phpredis

# Check whether the redis extension is installed on the
root@517b9c67507a:/tmp# php -m | grep redis
redis
root@517b9c67507a:/tmp#

docker-php-ext-configure

Docker PHP ext configure is generally used in combination with docker PHP ext install. Its function is to help you do it when you need to customize the configuration when you install the extension.

case

FROM php:7.1-fpm
RUN apt-get update \
    # Related dependencies must be installed manually
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    # Install extensions
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    # If the installed extension requires custom configuration
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

Posted by ChrisA on Mon, 08 Nov 2021 04:34:42 -0800