在开发中,PHP 和 mysql 经常配合使用,那么在使用 docker 的时候 php 镜像默认是不带这个扩展的,如何进行安装呢?其实官方给出了说明:
How to install more PHP extensions
Many extensions are already compiled into the image, so it's worth checking the output of
php -m
orphp -i
before going through the effort of compiling more.
We provide the helper scripts
docker-php-ext-configure
,docker-php-ext-install
, anddocker-php-ext-enable
to more easily install PHP extensions.
In order to keep the images smaller, PHP's source is kept in a compressed tar file. To facilitate linking of PHP's source with any extension, we also provide the helper script
docker-php-source
to easily extract the tar or delete the extracted source. Note: if you do usedocker-php-source
to extract the source, be sure to delete it in the same layer of the docker image.
FROM php:7.4-cli
RUN docker-php-source extract \
# do important things \
&& docker-php-source delete
嗯,又是一大段英文,大概说了但是我不想重新生成镜像来用。网上找到这段说明:
命令参数作用docker-php-sourceextract | delete加载、移除扩展文件docker-php-ext-installext安装并启用扩展docker-php-ext-enableext启用扩展docker-php-ext-configure没用过不知道命令
这些命令可以直接在镜像中使用:
开启 php
docker run --name php -v path:/code -p 8080:8000 -it php
启用 mysql 扩展
docker-php-source extract
cd /usr/src/php/ext
docker-php-ext-install pdo_mysql
docker-php-source delete