如何使用正式的 PHP Docker 映像方法安装 PHP-redis 扩展?

我想用基于 正式的 PHP Docker 图像php-redis扩展构建 PHP-FPM 映像,例如,使用这个 Dockerfile: Php: 5.6-fpm

文档说我可以通过这种方式安装扩展,手动安装扩展的依赖项:

FROM php:5.6-fpm
# Install modules (iconv, mcrypt and gd extensions)
RUN apt-get update && apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng12-dev \
&& docker-php-ext-install iconv mcrypt \
&& docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/ \
&& docker-php-ext-install gd
CMD ["php-fpm"]

在没有 Docker 的情况下,我使用 apt-get install php5-redis安装了它。但是如何使用上面的方法安装它呢?

99929 次浏览

Redis is not an extension that is included in “php-src”, therefore you cannot use docker-php-ext-install. Use PECL:

RUN pecl install -o -f redis \
&&  rm -rf /tmp/pear \
&&  docker-php-ext-enable redis

On alpine php 7.3.5 we can use:

RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so

I've found two ways to install php-redis extension for official php-fpm Docker image. Here they are:

The first way is to compile redis from sources and install.

RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/2.2.7.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-2.2.7 /usr/src/php/ext/redis \
&& docker-php-ext-install redis

docker-php-ext-install script is included in php-fpm image and can compile extensions and install them.

The second way you can do it is with PECL.

As TimWolla answered, you can do it with PECL, but in my case, PECL isn't installed by default.

RUN pecl install -o -f redis \
&&  rm -rf /tmp/pear \
&&  echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini

Based on @starikovs answer. I added a variable for docker style.

# install phpredis extension
ENV PHPREDIS_VERSION 2.2.7


RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis

If you want to use redis as session handler;

RUN { \
echo 'session.save_handler = redis'; \
echo 'session.save_path = tcp://redis:6379'; \
} >> /usr/local/etc/php/conf.d/docker-php-ext-redis.ini

If you want to use redis extension with PHP 7 in 2015 (borrowed from skyred's answer);

ENV PHPREDIS_VERSION php7


RUN curl -L -o /tmp/redis.tar.gz https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz \
&& tar xfz /tmp/redis.tar.gz \
&& rm -r /tmp/redis.tar.gz \
&& mv phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& docker-php-ext-install redis

I'm using combination of PECL and PHP official docker extension script

RUN pecl bundle -d /usr/src/php/ext redis \
&& rm /usr/src/php/ext/redis-*.tgz \
&& docker-php-ext-install redis

For PHP7 you need to wait for official redis pecl release or use git:

RUN apt-get update \
&& apt-get install git -y -q \
&& git clone -b php7 https://github.com/phpredis/phpredis.git /usr/src/php/ext/redis \
&& docker-php-ext-install redis

Slightly revised version of starikovs and skyred answers for current version of the docker image. Tested on php:5-fpm-alpine

# install phpredis extension
ENV PHPREDIS_VERSION 2.2.8


ADD https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz /tmp/redis.tar.gz
RUN tar xzf /tmp/redis.tar.gz -C /tmp \
&& mkdir -p /usr/src/php/ext \
&& mv /tmp/phpredis-$PHPREDIS_VERSION /usr/src/php/ext/redis \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis \
&& rm -rf /usr/src/php/ext/redis

Slightly revised version of starikovs and skyred answers for the current PHP 7 version of the docker image (tested on php:7.0.8-fpm-alpine and php:7.0.8-alpine).

Uses the newly released 3.0 version (June 2016) for PHP 7.

ENV PHPREDIS_VERSION 3.0.0


RUN mkdir -p /usr/src/php/ext/redis \
&& curl -L https://github.com/phpredis/phpredis/archive/$PHPREDIS_VERSION.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 \
&& echo 'redis' >> /usr/src/php-available-exts \
&& docker-php-ext-install redis

My opinion, the easiest way is:

RUN pecl install redis && docker-php-ext-enable redis

;)

In your Dockerfile you can clone the repo and install it with:

RUN git clone https://github.com/phpredis/phpredis.git /tmp/phpredis \
&& cd /tmp/phpredis \
&& git checkout -b 3.1.2 \ ## or the release you need #
&& phpize \
&& ./configure \
&& make \
&& make install

This works for alpine images:

RUN set -xe \
&& apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
&& pecl install -o -f redis  \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini \
&& rm -rf /usr/share/php \
&& rm -rf /tmp/* \
&& apk del  .phpize-deps

Edit: Added missing backslash

Tried few ways. On alpine php 7.3.5 we can use:

RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so

For image php:7.2-fpm-alpine.

RUN apk add autoconf gcc g++ make && pecl install redis && docker-php-ext-enable redis

You may need to update before

apk --update upgrade