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

前端 未结 11 1007
Happy的楠姐
Happy的楠姐 2021-01-30 02:21

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.

The docs

相关标签:
11条回答
  • 2021-01-30 02:38

    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
    
    0 讨论(0)
  • 2021-01-30 02:39

    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
    
    0 讨论(0)
  • 2021-01-30 02:41

    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
    
    0 讨论(0)
  • 2021-01-30 02:42

    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
    
    0 讨论(0)
  • 2021-01-30 02:45

    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
    
    0 讨论(0)
  • 2021-01-30 02:47

    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
    
    0 讨论(0)
提交回复
热议问题