[英]How to install php-redis extension using the official PHP Docker image approach?
I want to build my PHP-FPM image with php-redis
extension based on the official PHP Docker image , for example, using this Dockerfile: php:5.6-fpm .我想使用基于官方 PHP Docker 镜像的
php-redis
扩展构建我的 PHP-FPM 镜像,例如,使用这个 Dockerfile: php:5.6-fpm 。
The docs say that I can install extensions this way, installing dependencies for extensions manually:文档说我可以通过这种方式安装扩展,手动安装扩展的依赖项:
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"]
Without Docker I installed it with apt-get install php5-redis
.在没有 Docker 的情况下,我使用
apt-get install php5-redis
安装了它。 But how can I install it using the approach above?但是如何使用上述方法安装它?
My opinion, the easiest way is:我认为,最简单的方法是:
RUN pecl install redis && docker-php-ext-enable redis
;) ;)
I've found two ways to install php-redis extension for official php-fpm Docker image .我找到了两种为官方php-fpm Docker 镜像安装php-redis扩展的方法。 Here they are:
他们来了:
The first way is to compile redis from sources and install.第一种方式是从源码编译redis并安装。
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. docker-php-ext-install
脚本包含在 php-fpm 镜像中,可以编译和安装扩展。
The second way you can do it is with PECL.第二种方法是使用 PECL。
As TimWolla answered , you can do it with PECL , but in my case, PECL isn't installed by default.正如 TimWolla回答的那样,您可以使用PECL 来完成,但就我而言,默认情况下未安装 PECL。
RUN pecl install -o -f redis \
&& rm -rf /tmp/pear \
&& echo "extension=redis.so" > /usr/local/etc/php/conf.d/redis.ini
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
).对当前 PHP 7 版本的
php:7.0.8-fpm-alpine
映像(在php:7.0.8-fpm-alpine
和php:7.0.8-alpine
)的starikovs和skyred 的答案略有修改。
Uses the newly released 3.0
version (June 2016) for PHP 7.使用新发布的 PHP 7
3.0
版本(2016 年 6 月)。
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
Based on @starikovs answer.基于@starikovs 的回答。 I added a variable for docker style.
我为 docker 风格添加了一个变量。
# 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;如果您想使用 redis 作为会话处理程序;
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);如果你想在 2015 年使用 PHP 7 的 redis 扩展(借用skyred的回答);
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
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:
在 alpine php 7.3.5 上,我们可以使用:
RUN apk add --no-cache pcre-dev $PHPIZE_DEPS \
&& pecl install redis \
&& docker-php-ext-enable redis.so
I'm using combination of PECL and PHP official docker extension script我正在使用 PECL 和 PHP 官方 docker 扩展脚本的组合
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:对于 PHP7,您需要等待官方 redis pecl 发布或使用 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.对当前版本的docker 映像稍微修改了starikovs和skyred 的答案。 Tested on php:5-fpm-alpine
在 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
In your Dockerfile you can clone the repo and install it with:在您的 Dockerfile 中,您可以克隆存储库并使用以下命令安装它:
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
For image php:7.2-fpm-alpine.对于图像 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.