(学习到实践)三、docker自定义php镜像

前言

制作以官方的alpine3.9版为基础代码,添加自定义插件。

1.编辑原则

使用官方Dockerfile,去除认证、校验方面的东西,合并RUN代码减少不必要的层数,启动脚本的替换。

2.分步制作

由于之前使用官方时总是遇到各种错误,为了提高完成效率,分步制作镜像。

a.纯php带各种中介软件和dev

代码:

# php7.3.5; Feb 7, 2019 link: https://github.com/docker-library/php/blob/master/7.3/alpine3.9/fpm/Dockerfile
# Base images 基础镜像+阿里源
FROM alpine:3.9

#MAINTAINER 维护者信息
MAINTAINER cffycls@foxmail.com

# dependencies required for running "phpize"
ENV PHP_VERSION 7.3.6
ENV PHP_URL https://secure.php.net/get/php-$PHP_VERSION.tar.xz/from/this/mirror
ENV PHPIZE_DEPS \
        autoconf \
        dpkg-dev dpkg \
        file \
        g++ \
        gcc \
        libc-dev \
        make \
        pkgconf \
        re2c
ENV PHPIZE_DEVS \
        argon2-dev \
        coreutils \
        curl-dev \
        libedit-dev \
        libsodium-dev \
        libxml2-dev \
        openssl-dev \
        sqlite-dev \
        libjpeg-turbo-dev \
        libpng-dev \
        gd-dev \
        gettext-dev \
        freetype-dev \
        libxpm-dev \
        libevent-dev

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories \
    && apk update \
    && addgroup -g 82 -S www-data \
    && adduser -u 82 -D -S -G www-data www-data \
    && mkdir -p "/usr/local/etc/php/conf.d" && mkdir -p "/var/www/html" \
    && chown www-data:www-data /var/www/html && chmod 777 /var/www/html \
    && apk add --no-cache\
        curl \
        tar \
        xz \
        openssl \
        wget 

COPY php.tar.xz php.tar.xz
RUN set -eux; \
        apk add $PHPIZE_DEPS $PHPIZE_DEVS \
            
        # && wget -O php.tar.xz "$PHP_URL" \
        && tar -Jxf php.tar.xz && cd php-$PHP_VERSION && ./configure \
        --prefix="/usr/local/php" \
        --with-config-file-path="/usr/local/php/etc" \
        --with-config-file-scan-dir="/usr/local/php/etc/conf.d" \
        \
        --enable-option-checking=fatal \
        --with-mhash \
        \
        --enable-ftp \
        --enable-exif \
        --enable-mbregex \
        --enable-mbstring \
        --enable-mysqlnd \
        --enable-sysvmsg \
        --enable-opcache \
        --enable-pcntl \
        --enable-sockets \
        --enable-sysvsem \
        --enable-xml \
        --with-curl \
        --with-libedit \
        --with-openssl \
        --with-zlib \
        --with-pcre-regex \
        --with-pear \
        --with-libxml-dir=/usr \
        --with-jpeg-dir \
        --with-freetype-dir \
        --with-xpm-dir \
        --with-png-dir \
        --with-gettext \
        --with-mhash \
        --with-iconv \
        --disable-fileinfo \
        \
        --enable-fpm --with-fpm-user=www-data --with-fpm-group=www-data --disable-cgi \
    && make -j "$(nproc)" \
    && find -type f -name '*.a' -delete \
    && make install \
#    && make clean \
    && rm -rf /tmp/pear ~/.pearrc \
    && cd ../ && rm -rf php-$PHP_VERSION.tar.xz php-$PHP_VERSION

b.测试准备

build上面镜像,运行所得容器,准备添加扩展。容器启动时所需配置文件/usr/local/php/etc/文件夹需要准备一个配置共享,这里取上面官方的配置稍加修改,并共享出来供后续安装使用:

[]:~/tmp/dk# tree -a php
php
├── config
│   ├── pear.conf
│   ├── php-fpm.conf
│   ├── php-fpm.conf.default
│   ├── php-fpm.d
│   │   ├── www.conf
│   │   └── www.conf.default
│   ├── php.ini
│   └── start.sh
├── Dockerfile
└── php.tar.xz 

以上配置修改成常用配置。

c.添加扩展,问题分解

首先考虑官方pecl安装插件,这里目标:

swoole-inotify-redis-uuid-memcached
#需要自行下载类库的插件或交互式安装的,使用单独下载源码编译的方式。

下面是在运行测试shell安装,并转换到Dockerfile的代码的结果。其中在安装memcached(相当于是memcache的新版、强化版)时遇到问题,官方memcached很快安装成功但php扩展难装:多次出现编译错误,php的memcached报缺libmemcached,而后者总是无法通过
参考《错误解决》,使用其中的参考自:https://bugs.launchpad.net/li…

Bug Description
When building with latest GCC version 7
----------

clients/memflush.cc:42:22: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
   if (opt_servers == false)
                      ^~~~~
clients/memflush.cc:51:24: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
     if (opt_servers == false)
                        ^~~~~
Trivial fix:
http://pkgs.fedoraproject.org/cgit/rpms/libmemcached.git/plain/libmemcached-build.patch
#打补丁,兴奋半天没有效果
http://pkgs.fedoraproject.org/cgit/rpms/libmemcached.git/plain/libmemcached-build.patch
patch -p0 < libmemcached-build.patch

最终使用 apk add libmemcached-dev 安装成功!!!(apk add libmemcached,或search的各版本都不行,只要一个dev)

d.扩展添加的结果初稿

#测试

#swoole 需要对话参数,所以自定义安装
RUN set -ex && \
    mkdir -p ~/build/swoole && \
    cd ~/build/swoole && \
    wget -O swoole.tar.gz https://github.com/swoole/swoole-src/archive/master.tar.gz && \
    tar zxvf swoole.tar.gz --strip-components 1 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config \
    --enable-coroutine \
    --enable-openssl  \
    --enable-http2  \
    --enable-async-redis \
    --enable-sockets \
    --enable-mysqlnd && \
    make && make install && \
    cd ../ && rm -rf swoole*

#inotify
RUN set -ex && \
    mkdir -p ~/build/inotify && \
    cd ~/build/inotify && \
    wget -O inotify.tgz https://pecl.php.net/get/inotify-2.0.0.tgz && \
    tar -zxf inotify.tgz --strip-components 1 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config --enable-inotify && \
    make && make install && \
    cd .. && rm -rf inotify*

#redis
RUN set -ex && \
    mkdir -p ~/build/redis && \
    cd ~/build/redis && \
    wget -O redis.tgz https://pecl.php.net/get/redis-4.3.0.tgz && \
    tar -zxf redis.tgz --strip-components 1 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config --enable-redis && \
    make && make install && \
    cd .. && rm -rf redis*

#uuid
RUN set -ex && \
    mkdir -p ~/build/libuuid && \
    cd ~/build/libuuid && \
    wget -O libuuid.tgz "http://nchc.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz" && \
    tar -zxf libuuid.tgz --strip-components 1 && \
    ./configure --prefix=/usr && \
    make && make install && \
    cd ../ && rm -rf libuuid* && \
    \
    wget -O uuid.tgz http://pecl.php.net/get/uuid-1.0.4.tgz && \
    tar zxf uuid.tgz --strip-components 1 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config && \
    make && make install && \
    cd ../ && rm -rf uuid*

#memcached
RUN set -ex && \
    #测试命令:/usr/bin/memcached -d -m 1024 -u root -l 0.0.0.0 -p 11211 -c 1024 -P /tmp/memcached.pid 启动正常
    mkdir -p ~/build/memcached && \
    cd ~/build/memcached && \
    wget -O memcached.tgz "http://memcached.org/files/memcached-1.5.16.tar.gz" && \
    tar -zxf memcached.tgz --strip-components 1 && \
    ./configure --with-event-libevent-dir=/usr --prefix=/usr && \
    make && make install && \
    cd ../ && rm -rf memcached* && \
    \
    #需要libmemcached
    apk add libmemcached-dev && \
    \
    mkdir -p ~/build/memcached_p && \
    cd ~/build/memcached_p && \
    wget -O memcached.tgz "https://pecl.php.net/get/memcached-3.1.3.tgz" && \
    tar -zxf memcached.tgz --strip-components 1 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config && \
    make && make install && \
    cd ../ && rm -rf memcached_p*

#编译使用,运行时共享 -v /your_real_path/ /usr/local/php/etc/
COPY config /usr/local/php/etc
VOLUME ["/usr/local/php/etc","/var/www/html"]

RUN set -ex \
    && /usr/local/php/bin/pecl channel-update pecl.php.net \
    && /usr/local/php/bin/pecl install igbinary event \
    && /usr/local/php/bin/php -m

d.脚本结尾设置

CMD ["/usr/local/php/etc/start.sh"]

3.分步提交到云

a.提交代码

[]:~/tmp/dk/php# docker push cffycls/php7:v0.9
The push refers to repository [docker.io/cffycls/php7]
... ...

等待上传完成。
这里镜像提交达到 519M,官方的是 367M,排除memcached的安装,还有不少依赖有待简化,之后测试这个环节还会修改完善。

    原文作者:沧浪水
    原文地址: https://segmentfault.com/a/1190000019593267
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞