How can I install the php memcached extension on Docker's PHP7 Alpine image?

timetofly picture timetofly · Nov 30, 2016 · Viewed 26.8k times · Source

The official php7 docker image has the following example:

FROM php:7.0-fpm
RUN apt-get update && apt-get install -y libmemcached-dev \
    && pecl install memcached \
    && docker-php-ext-enable memcached

I'm trying to use FROM php:7.0-fpm-alpine:

RUN apk add --update --no-cache libmemcached-dev
RUN      pecl install memcached && docker-php-ext-enable memcached

PECL gives this error:

pecl/memcached requires PHP (version >= 5.2.0, version <= 6.0.0, excluded versions: 6.0.0), installed version is 7.0.13

How can I install the memcached php extension on alpine?

Answer

Harry Bragg picture Harry Bragg · Jan 10, 2017

Currently the php-memcached-dev:php7 branch contains the source for this extension.

To install it you can still use the docker-php-ext-* commands but you need to checkout the source yourself.

Base Installation

Assuming everything required to install the extension is already installed you can do:

RUN git clone -b php7 https://github.com/php-memcached-dev/php-memcached /usr/src/php/ext/memcached \
    && docker-php-ext-configure /usr/src/php/ext/memcached \
        --disable-memcached-sasl \
    && docker-php-ext-install /usr/src/php/ext/memcached \
    && rm -rf /usr/src/php/ext/memcached

This block will clone the repository, configure and install the extension then clean up after it self.

Pre-requisites

It is most likely that you need to install to packages to build the extension, we can add and remove them by doing:

ENV MEMCACHED_DEPS zlib-dev libmemcached-dev cyrus-sasl-dev git
RUN set -xe \
    && apk add --no-cache libmemcached-libs zlib \
    && apk add --no-cache \
        --virtual .memcached-deps \
        $MEMCACHED_DEPS \
    && git clone -b php7 https://github.com/php-memcached-dev/php-memcached /usr/src/php/ext/memcached \
    && docker-php-ext-configure /usr/src/php/ext/memcached \
        --disable-memcached-sasl \
    && docker-php-ext-install /usr/src/php/ext/memcached \
    && rm -rf /usr/src/php/ext/memcached \
    && apk del .memcached-deps

Update 17 May 2017

memcached has been added to the official pecl libraries for php7 now (v3 -> php7/7.1, v2 -> php5)

This makes installation a bit different

FROM php:7.0-alpine

ENV MEMCACHED_DEPS zlib-dev libmemcached-dev cyrus-sasl-dev
RUN apk add --no-cache --update libmemcached-libs zlib
RUN set -xe \
    && apk add --no-cache --update --virtual .phpize-deps $PHPIZE_DEPS \
    && apk add --no-cache --update --virtual .memcached-deps $MEMCACHED_DEPS \
    && pecl install memcached \
    && echo "extension=memcached.so" > /usr/local/etc/php/conf.d/20_memcached.ini \
    && rm -rf /usr/share/php7 \
    && rm -rf /tmp/* \
    && apk del .memcached-deps .phpize-deps