Detailed steps Docker php mounting extension

Foreword

Cipian, mainly demonstration docker-php-source, docker- php-ext-install, docker-php-enable-docker-configure these four commands are used to doing in the end, they are in PHP container have done what thing.
Many people did not understand PHP extensions installed in Dockerfile is always present when these orders herein, this is for you to uncover the mystery of these commands are prepared, in all cases are my own run-off.

The PHP Docker container mounting methods are extensions
  1. Pecl install by way of
  2. Php mounted by the container carrying several special command, these special commands may be used in Dockerfile the RUN command.

Here, we focus on is the second program, how to install PHP extensions PHP container by several special command

PHP is installed extensions There are several special command

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

Demonstrate the role of these three commands

PHP is making a presentation in a container, the container PHP start too simple, without much introduction

docker-php-source

This command is, in fact, is to create a / usr / src / php directory in the PHP container, which put some file that comes with it. We take it as a download from the Internet down PHP extensions source storage directory. In fact, all PHP extensions source storage expansion path: / usr / src / php / ext inside.

Format :

docker-php-source extract | delete

Parameter Description :
* Extract: to create and initialize / usr / src / php directory
* delete: delete / usr / src / php directory

Case :

root@803cbcf702a4:/usr/src# ls -l
total 11896 #此时,并没有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 extract
root@803cbcf702a4:/usr/src# ls -l
total 11900 #此时,生产了php目录,里面还有一些文件,由于篇幅问题,就不进去查看了
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 #此时,将已创建 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-ext-enable

This command is used to start PHP extension of. We use pecl install PHP extensions, the default is not enabled this extension, if you want to use this extension must be in the php.ini configuration file to configure it to use this PHP extension. The docker-php-ext-enable command is automatically given us to start the PHP extension, you do not need to go to the php.ini configuration file to configure.
Case

# 查看现有可以启动的扩展
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#

# 查看redis 扩展是否可以启动
root@517b9c67507a:/usr/local/etc/php# php -m | grep redis
root@517b9c67507a:/usr/local/etc/php#

# 启动 redis 扩展
root@517b9c67507a:/usr/local/etc/php# docker-php-ext-enable redis
# 启动 成功
root@517b9c67507a:/usr/local/etc/php# php -m | grep redis
redis
root@517b9c67507a:/usr/local/etc/php#

#说明,php容器中默认是没有php.ini配置文件的,加载原理如下所示

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)
# 核心是 /usr/local/etc/php/conf.d 目录下的扩展配置文件
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 the PHP extension of.
Format:
Docker-PHP-EXT-install "source package directory name"

important point:

  • "Source package" needs to be placed / usr / src / php / ext down
  • By default, PHP container is not / usr / src / php this directory, use docker-php-source extract is generated.
  • docker-php-ext-install extensions installed after the installation is complete, it will automatically call docker-php-ext-enable the expansion to start the installation.
  • Removing expansion, delete /usr/local/etc/php/conf.d corresponding configuration file.

Case

# 卸载redis 扩展
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#

#PHP容器默认是没有redis扩展的。所以我们通过docker-php-ext-install安装redis扩展

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

#检查移过去的插件源码包是否存在
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

# 检查redis 扩展是否已经安装上
root@517b9c67507a:/tmp# php -m | grep redis
redis
root@517b9c67507a:/tmp#

docker-php-ext-configure

docker-php-ext-configure usually need to match with docker-php-ext-install use. Its role is that when you install the extension, the need for custom configuration, you can use it to help you do it.

Case

FROM php:7.1-fpm
RUN apt-get update \
    # 相关依赖必须手动安装
    && apt-get install -y \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libmcrypt-dev \
        libpng-dev \
    # 安装扩展
    && docker-php-ext-install -j$(nproc) iconv mcrypt \
    # 如果安装的扩展需要自定义配置时
    && docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
    && docker-php-ext-install -j$(nproc) gd

Guess you like

Origin www.cnblogs.com/yinguohai/p/11329273.html