如何使用官方 PHP Docker 映像方法安装 php-redis 扩展?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31369867/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:24:45  来源:igfitidea点击:

How to install php-redis extension using the official PHP Docker image approach?

phpredisdockerdockerfile

提问by starikovs

I want to build my PHP-FPM image with php-redisextension based on the official PHP Docker image, for example, using this Dockerfile: php:5.6-fpm.

我想使用php-redis基于官方 PHP Docker 镜像的扩展来构建我的 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. But how can I install it using the approach above?

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

采纳答案by starikovs

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

我找到了两种为官方php-fpm Docker 镜像安装php-redis扩展的方法。他们来了:

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-installscript 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

回答by TimWolla

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

Redis 不是“php-src”中包含的扩展,因此您不能使用docker-php-ext-install. 使用PECL

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

回答by Pavel

My opinion, the easiest way is:

我认为,最简单的方法是:

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

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

;)

;)

回答by otherguy

Slightly revised version of starikovsand skyredanswers for the current PHP 7 version of the docker image (tested on php:7.0.8-fpm-alpineand php:7.0.8-alpine).

对当前 PHP 7 版本的docker镜像(在和 上测试)略有修改的starikovsskyred答案。php:7.0.8-fpm-alpinephp:7.0.8-alpine

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

使用新发布3.0的 PHP 7 版本(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

回答by skyred

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

基于@starikovs 的回答。我为 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

回答by masakielastic

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

回答by grogowar

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

编辑:添加缺少的反斜杠

回答by isp_developer

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

回答by selim13

Slightly revised version of starikovsand skyredanswers for current version of the docker image. Tested on php:5-fpm-alpine

对当前版本的docker 映像稍微修改了starikovsskyred 的答案。在 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

回答by azuax

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