简体   繁体   中英

PHP7 fails to load memcached and redis on Alpine docker container

I'm trying to create a Docker image based on Alpine Linux which will run PHP 7.1 ( apk add php7=7.1.9-r0 ) with some modules installed ( memcached , mongodb , oauth , openssl and redis ).

I install the modules through PECL like this:

RUN yes | pecl install \
  igbinary \
  redis-3.1.4 \
  oauth-2.0.2 \
  memcached-3.0.4 \
  mongodb-1.3.3

Then add each of them to php.ini .

RUN for EXT in \
    igbinary \
    memcached \
    mongodb \
    oauth \
    openssl \
    redis; \
  do \
    echo "extension=${EXT}.so" >> /etc/php7/php.ini; \
  done

Most modules install correctly, but memcached and redis don't want to play along:

# php -v
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php7/modules/memcached.so' - Error relocating /usr/lib/php7/modules/memcached.so: php_session_create_id: symbol not found in Unknown on line 0
PHP Warning:  PHP Startup: Unable to load dynamic library '/usr/lib/php7/modules/redis.so' - Error relocating /usr/lib/php7/modules/redis.so: php_session_register_module: symbol not found in Unknown on line 0
PHP Warning:  Module 'openssl' already loaded in Unknown on line 0
PHP Warning:  Cannot load module 'mongodb' because required module 'json' is not loaded in Unknown on line 0
PHP 7.1.9 (cli) (built: Oct  2 2017 20:51:54) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.1.0, Copyright (c) 1998-2017 Zend Technologies

I've also tried from source:

RUN git clone https://github.com/php-memcached-dev/php-memcached
RUN cd php-memcached \
  && git checkout php7 \
  && git pull \
  && /usr/bin/phpize \
  && ./configure --with-php-config=/usr/bin/php-config \
  && make \
  && make install

This however gives me the same result. I've done some searching and apparently there might be some related problem of glibc on Alpine ( example thread ) but I'm not quite sure this is the same issue as the error output is a bit confusing.

Is there anything I'm overlooking as to how these modules should be installed to work with PHP 7.1 on Alpine Linux?

All of these extensions are available in Alpine repositories, so why are you making your life harder and installs them directly from PECL? Install them simply using apk .

Note that these packages are in Alpine v3.7 (the latest stable release), I haven't checked if they are available also in older releases.

These packages, of course, installs config files with extension=<ext>.so , so don't add it manually to php.ini .


apk add php7=7.1.9-r0

Why do you specify exact version? This will fail once we update the package (eg with security patches), because only the latest version of packages is available in the repositories. We backport only security fixes and bugfixes (ie patch versions) into stable releases, so there will not be 7.2.x in v3.6 or v3.7.

PHP 7.2 And onward are based on Alpine 3.7 Wich has all the necessary extensions available in the repository. However there are PHP 7.1 and PHP 7.0 Which are still based on 3.4 which does not have any php7-* extensions.

Workaround is to install pecl and which in turn can install all necessary extensions.

You can achieve this in this way:

RUN apk update\
  && apk upgrade \
  && apk add libmemcached \
    libmemcached-libs \
    libmemcached-dev \
    build-base \
    zlib-dev \
    php5-dev \
    git \
    autoconf \
    cyrus-sasl-dev \
  && pecl config-set php_ini  /usr/local/etc/php/php.ini \
  && pecl install -f memcached \ #Add any Additional packages
  && echo extension=memcached.so >> /usr/local/etc/php/conf.d/docker-php-ext-memcached.ini \
  && rm -rf /tmp/pear \
  && apk del php5-dev \
     build-base \
     zlib-dev \
     php5-dev \
     git \
     autoconf \
     cyrus-sasl-dev

This will install PECL with php5 which works perfectly to install extensions for PHP 7+

And dont forget to include your packages

We were facing similar issues with the "official" PHP images build on Alpine. Ie. it was impossible for us to install a working ImageMagick version in PHP 7.1 which is based on Alpine 3.4

What we did is installing it from Alpine 3.6 , while I actually won't recommend this it might be a workaround.

The other workaround is to wait for PHP 7.2 which is build on Alpine 3.6. Might be an option to look for a working PHP + Alpine combination, since you are building FROM alpine and not FROM php:alpine

Just saying: Issues like that made us go back to Debian images, since we've wasted tons of hours on that, including weird glibc issues like you mentioned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM