Installing php extension in docker

Keywords: PHP Docker Redis Nginx

Some extensions of php

Using the keyword docker PHP ext install directly in the Dockerfile file

 

RUN 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) gd
  && docker-php-ext-install pdo_mysql \
  && docker-php-ext-install bcmath \
  && docker-php-ext-install sockets \
  && docker-php-ext-install zip \
  && docker-php-ext-install sysvmsg 

Extension of pecl

On this site https://pecl.php.net You can use pecl install to install the extension of, such as redis swoole. When you use this command to install, you need to pay attention to whether there are pecl commands in the image. If not, install

 

RUN pecl install swoole-4.2.12 \
    && docker-php-ext-enable swoole \
    && pecl install inotify-2.0.0 \
    && docker-php-ext-enable inotify 

PECL will install from the https://pecl.php.net Download the package and install it, so the following methods are also available.

 

RUN wget http://pecl.php.net/get/redis-${PHPREDIS_VERSION}.tgz -O /tmp/redis.tar.tgz \
    && pecl install /tmp/redis.tar.tgz \
    && rm -rf /tmp/redis.tar.tgz \
    && docker-php-ext-enable redis

Download installation package installation extension

Some extensions are not available in the above two ways or want to be installed with source code, you can use this way. In fact, the above two methods are essentially download and installation methods, so you should also confirm whether phpize and other commands exist. If you do not configure the environment variables of phpize, you have to write the full path. , also check to see if the make command is installed

Here is how to download swoole extension from swoft

 

RUN wget https://github.com/swoole/swoole-src/archive/v${SWOOLE_VERSION}.tar.gz -O swoole.tar.gz \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && rm swoole.tar.gz \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-mysqlnd --enable-sockets --enable-openssl --enable-http2 \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r swoole \
    && docker-php-ext-enable swoole 

Here's how to download and extend php official website

 

FROM php:5.6-cli
RUN curl -fsSL 'https://xcache.lighttpd.net/pub/Releases/3.2.0/xcache-3.2.0.tar.gz' -O xcache.tar.gz \
    && mkdir -p xcache \
    && tar -xf xcache.tar.gz -C xcache --strip-components=1 \
    && rm xcache.tar.gz \
    && ( \
        cd xcache \
        && phpize \
        && ./configure --enable-xcache \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r xcache \
    && docker-php-ext-enable xcache

For more detailed usage, see docker php image description
The following points need to be paid attention to when using this method:

  1. The last command of RUN cannot have a slash, otherwise an error is reported, as follows

 

&& docker-php-ext-enable xcache \
  1. The suffix of the downloaded package must be the same as that after decompression, otherwise an error will be reported during decompression
  2. The first parameter in wget is - O, not - O, uppercase. Wrong on docker hub

problem

  • Question 1

 

Cannot find autoconf. Please check your autoconf installation and the
$PHP_AUTOCONF environment variable. Then, rerun this script.

ERROR: `phpize' failed

Solution, install autoconf. centos uses yum, unbantu uses apt get, and alpine uses apk

 

RUN apk update \
    && apk add autoconf \
    && docker-php-ext-install pcntl \
    && pecl install redis-5.0.0 \
    && docker-php-ext-enable redis
COPY . /var/www/html
COPY ./docker/nginx-php7/sites-enabled /etc/nginx/sites-enabled
  • Question 2
    no acceptable C compiler found in $PATH

 

configure: error: in `/var/www/html/redis':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details

The solution is to install gcc in

 

apk add gcc
  • Question 3
    The same error is reported in. / configure. C compiler cannot create executables

 

configure: error: in `/var/www/html/redis':
configure: error: C compiler cannot create executables
See `config.log' for more details

Because I am using the alpine image of docker, the solution is as follows: Reference resources , the solution is as follows:

 

apk add gcc g++

 


The above three problems are encountered during the installation of redis extension. The Dockerfile is as follows

 

 

FROM richarvey/nginx-php-fpm:latest

RUN apk update \
    && apk add autoconf \
    && apk add gcc g++\
    && apk add make \
    && docker-php-ext-install pcntl \
    && wget https://pecl.php.net/get/redis-5.0.1.tgz -O redis.tgz \
    && mkdir -p redis \
    && tar -xf redis.tgz -C redis --strip-components=1 \
    && rm redis.tgz \
    && ( \
        cd redis \
        && phpize \
        && ./configure --with-php-config=/usr/local/bin/php-config \
        && make -j$(nproc) \
        && make install \
    ) \
    && rm -r redis \
    && docker-php-ext-enable redis
COPY . /var/www/html
COPY ./docker/nginx-php7/sites-enabled /etc/nginx/sites-enabled

 

77 original articles published, 96 praised, 350000 visitors+
Private letter follow

Posted by Tristan Wells on Sat, 14 Mar 2020 23:41:05 -0700