使用 Docker 安装 PHP7 fpm 和 memcached

Install PHP7 fpm and memcached with Docker

我有一个带有 Docker 的应用程序,我正在尝试使用 php7-fpm 安装 memcached。

根据 official docker documentation 我的 Docker 文件中有:

# PHP Version
FROM php:7.0-fpm

...

# Install Memcached
RUN apt-get install -y libmemcached-dev && \
pecl install memcached && \
docker-php-ext-enable memcached

但是我得到了这个错误:

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

我不想切换到 PHP 5.6。有什么想法吗?

我们在构建 php7 容器时从头开始构建内存缓存扩展。也许我们的方法可以帮助您或为您指明正确的方向。 Dockerhub里面的文档好像真的有问题,试过pecl这里也没用。

这就是它在我们的 Dockerfile 中的样子:

RUN apt-get update && apt-get install -y 
        libmemcached11 \
        libmemcachedutil2 \
        libmemcached-dev \
        libz-dev \
        git \
    && cd /root \
    && git clone -b php7 https://github.com/php-memcached-dev/php-memcached \
    && cd php-memcached \
    && phpize \
    && ./configure \
    && make \
    && make install \
    && cd .. \
    && rm -rf  php-memcached \
    && echo extension=memcached.so >> /usr/local/etc/php/conf.d/memcached.ini \
    && apt-get remove -y build-essential libmemcached-dev libz-dev \
    && apt-get remove -y libmemcached-dev libz-dev \
    && apt-get autoremove -y \
    && rm -rf /var/lib/apt/lists/* \
    && apt-get clean

memcached 似乎与 php7 不兼容,需要另一种安装方式。

Laradock repo 快速锁定后我以这种方式解决了,我 post 代码:

# PHP Version
FROM php:7.0-fpm

# Install the PHP extensions we need
RUN apt-get update && \
apt-get install -y --no-install-recommends \
    curl \
    libmemcached-dev \
    libz-dev \
    libpq-dev \
    libjpeg-dev \
    libpng12-dev \
    libfreetype6-dev \
    libicu-dev \
    libssl-dev \
    libmcrypt-dev && \
    docker-php-ext-configure gd --with-png-dir=/usr --with-jpeg-dir=/usr && \
    docker-php-ext-install gd mysqli opcache intl

    .....

 # Install Memcached
RUN curl -L -o /tmp/memcached.tar.gz "https://github.com/php-memcached-   dev/php-memcached/archive/php7.tar.gz" && \
mkdir -p memcached && \
tar -C memcached -zxvf /tmp/memcached.tar.gz --strip 1 && \
( \
    cd memcached && \
    phpize && \
    ./configure && \
    make -j$(nproc) && \
    make install \
) && \
rm -r memcached && \
rm /tmp/memcached.tar.gz && \
docker-php-ext-enable memcached

还有一个解决方案

FROM php:7.2-fpm
# ...
# INSTALL memcached
RUN apt-get upgrade -y
RUN apt-get install -y memcached
RUN apt-get install -y libmemcached-dev zlib1g-dev libicu-dev
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