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

14

110

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?

Disentomb answered 12/7, 2015 at 16:15 Comment(0)
D
185

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

RUN pecl install --onlyreqdep --force 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
Dermott answered 12/7, 2015 at 16:17 Comment(9)
TimWolla, thanks, I'll try it. So, I need to install pecl as well before all of these?Disentomb
TimWolla, pecl isn't included by default.Disentomb
What if I want to use a different image for Redis?Borden
@Borden This command installs php extension for redis not redis server itselfFrailty
What do the -o and -f options do?Nakasuji
@LarsNyström -o --onlyreqdeps: install all required dependencies, -f --force: will overwrite newer installed packagesIjssel
Previously I made the mistake of using docker-php-ext-enable redis without first doing pecl install redis. As a result, phpinfo made it appear that redis was installed, because all of the .ini configuration was there, but the redis module itself was not. Thank you for you help!Compassionate
The last one worked fine for me on alpine 3.16 with PHP 8.0.Thrombo
"unrecognized option --onlyreqdep", seems there are no such option anymore in PECL comming with "wordpress:6.3-php8.2-apache"Nothing
O
76

My opinion, the easiest way is:

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

;)

Ossiferous answered 10/8, 2016 at 0:1 Comment(4)
Yes, it is a modern way answer.Timothy
Does NOT work. Specifically docker-php-ext-enable redis does NOT install any configuration files anywhere. It simply does nothing.Expertism
Gives me error docker-php-ext-enable -- not found. Can you help here ?Schooling
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!Portraitist
D
21

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
Disentomb answered 25/7, 2015 at 6:41 Comment(2)
I had to add a mkdir -p /usr/src/php/ext before the mv.Boole
I used pecl but had to add RUN apk add --no-cache autoconf git g++ make from this answer: https://mcmap.net/q/196305/-php7-redis-client-on-alpine-osLipo
T
21

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
Trait answered 25/7, 2016 at 12:6 Comment(1)
I tried out the answer from @Disentomb and I was never able to effectively enable the extension. This answer worked the first time!Jueta
B
11

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
Branen answered 25/8, 2015 at 7:21 Comment(0)
A
7

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
Appalling answered 22/9, 2015 at 10:16 Comment(0)
D
7

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
Denial answered 25/5, 2020 at 3:15 Comment(2)
Where is the variable $PHPIZE_DEPS set and what should be in it?Surplus
$PHPIZE_DEPTS set by docker alpine. and it use for build pecl extension like Redis php.net/manual/en/install.pecl.phpize.phpDenial
B
6

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

Bridie answered 29/5, 2019 at 6:18 Comment(0)
F
3

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
Folliculin answered 15/12, 2015 at 3:29 Comment(1)
Redis PECL extension v3 is available for PHP 7 since 2016-06-10.Planchet
L
1

This is a docker solution, use this code in Dockerfile.

COPY --from=mlocati/php-extension-installer /usr/bin/install-php-extensions /usr/local/bin/
RUN install-php-extensions redis

Image sources: https://github.com/mlocati/docker-php-extension-installer

Lepanto answered 3/4, 2023 at 12:55 Comment(1)
A little more detail on this (e.g. a link to the appropriate github repo) would be goodVector
H
0

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
Hagfish answered 19/7, 2016 at 12:24 Comment(0)
U
0

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
Understudy answered 23/3, 2019 at 18:19 Comment(0)
J
0

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
Jovitta answered 29/3, 2021 at 13:4 Comment(0)
L
0

Just RUN apk add --no-cache php83-pecl-redis for me.

Lazo answered 21/2 at 19:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.