Docker で PHP 拡張モジュールをインストールする | QUARTETCOM TECH BLOG

PHPのコンテナに入りbash起動

今回コンテナはPHP8.3を使う。

❯ docker run -it php:8.3 bash

有効モジュール一覧

デフォルトで有効になっているモジュールを確認:

# php -m
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
mysqlnd
openssl
pcre
PDO
pdo_sqlite
Phar
posix
random
readline
Reflection
session
SimpleXML
sodium
SPL
sqlite3
standard
tokenizer
xml
xmlreader
xmlwriter
zlib

[Zend Modules]

拡張モジュールのインストール

# docker-php-ext-install --help
usage: /usr/local/bin/docker-php-ext-install [-jN] [--ini-name file.ini] ext-name [ext-name ...]
   ie: /usr/local/bin/docker-php-ext-install gd mysqli
       /usr/local/bin/docker-php-ext-install pdo pdo_mysql
       /usr/local/bin/docker-php-ext-install -j5 gd mbstring mysqli pdo pdo_mysql shmop

if custom ./configure arguments are necessary, see docker-php-ext-configure

Possible values for ext-name:
bcmath bz2 calendar ctype curl dba dl_test dom enchant exif ffi fileinfo filter ftp gd gettext gmp hash iconv imap intl json ldap mbstring mysqli oci8 odbc opcache pcntl pdo pdo_dblib pdo_firebird pdo_mysql pdo_oci pdo_odbc pdo_pgsql pdo_sqlite pgsql phar posix pspell random readline reflection session shmop simplexml snmp soap sockets sodium spl standard sysvmsg sysvsem sysvshm tidy tokenizer xml xmlreader xmlwriter xsl zend_test zip

Some of the above modules are already compiled into PHP; please check
the output of "php -i" to see which modules are already loaded.

# docker-php-ext-install pdo_mysql
Configuring for:
PHP Api Version:         20230831
Zend Module Api No:      20230831
Zend Extension Api No:   420230831
...
Build complete.
Don't forget to run 'make test'.
...

インストールされているか確認:

# php -m | grep pdo_mysql
pdo_mysql

iniファイルが生成されているか確認:

# cat /usr/local/etc/php/conf.d/docker-php-ext-pdo_mysql.ini
extension=pdo_mysql.so

pecl install

mcrypt を pecl 経由でインストールしてみる:

# apt install -y libmcrypt-dev
# pecl install mcrypt
# docker-php-ext-enable mcrypt
# cat /usr/local/etc/php/conf.d/docker-php-ext-mcrypt.ini
extension=mcrypt
php > print_r(mcrypt_list_algorithms());

Deprecated: Function mcrypt_list_algorithms() is deprecated in php shell code on line 1
Array
(
    [0] => cast-128
    [1] => gost
    [2] => rijndael-128
    [3] => twofish
    [4] => arcfour
    [5] => cast-256
    [6] => loki97
    [7] => rijndael-192
    [8] => saferplus
    [9] => wake
    [10] => blowfish-compat
    [11] => des
    [12] => rijndael-256
    [13] => serpent
    [14] => xtea
    [15] => blowfish
    [16] => enigma
    [17] => rc2
    [18] => tripledes
)