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

82,960

Solution 1

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

Solution 2

My opinion, the easiest way is:

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

;)

Solution 3

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

Solution 4

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

Solution 5

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
Share:
82,960
starikovs
Author by

starikovs

Updated on November 25, 2021

Comments

  • starikovs
    starikovs over 2 years

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

  • starikovs
    starikovs almost 9 years
    TimWolla, thanks, I'll try it. So, I need to install pecl as well before all of these?
  • starikovs
    starikovs almost 9 years
    TimWolla, pecl isn't included by default.
  • emix
    emix almost 8 years
    Redis PECL extension v3 is available for PHP 7 since 2016-06-10.
  • Yarco
    Yarco almost 7 years
    Yes, it is a modern way answer.
  • Matthew Setter
    Matthew Setter almost 6 years
    I tried out the answer from @starikovs and I was never able to effectively enable the extension. This answer worked the first time!
  • Sivaji
    Sivaji over 5 years
    Works awesome!! Thank you.
  • Mouagip
    Mouagip over 4 years
    I had to add a mkdir -p /usr/src/php/ext before the mv.
  • IlGala
    IlGala over 4 years
    What if I want to use a different image for Redis?
  • piotrekkr
    piotrekkr over 4 years
    @IlGala This command installs php extension for redis not redis server itself
  • Szczepan Hołyszewski
    Szczepan Hołyszewski about 4 years
    Does NOT work. Specifically docker-php-ext-enable redis does NOT install any configuration files anywhere. It simply does nothing.
  • Lars Nyström
    Lars Nyström almost 4 years
    What do the -o and -f options do?
  • Sunil Pachlangia
    Sunil Pachlangia almost 4 years
    Gives me error docker-php-ext-enable -- not found. Can you help here ?
  • TomLi
    TomLi almost 4 years
    I used pecl but had to add RUN apk add --no-cache autoconf git g++ make from this answer: stackoverflow.com/a/44191638/4018940
  • kingjeffrey
    kingjeffrey over 3 years
    @LarsNyström -o --onlyreqdeps: install all required dependencies, -f --force: will overwrite newer installed packages
  • Tyler Collier
    Tyler Collier about 3 years
    I saw the comment by @SzczepanHołyszewski and just did the first command (RUN pecl install redis), not both (RUN pecl install redis && docker-php-ext-enable redis). That's all that was needed for me!
  • Calamity Jane
    Calamity Jane almost 3 years
    Where is the variable $PHPIZE_DEPS set and what should be in it?
  • mihnsen
    mihnsen almost 3 years
    $PHPIZE_DEPTS set by docker alpine. and it use for build pecl extension like Redis php.net/manual/en/install.pecl.phpize.php