(全棧學習實踐)3、建立php、添加擴展,其餘Dockerfile編寫

1、測試PHP
一、在官方的基礎上簡化:php

# 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

二、添加擴展:
這裏目標 swoole-inotify-redis-uuid-memcached,這裏是在運行上面鏡像的容器測試獲得結果,部分須要自行下載的插件或交互式安裝的,使用單獨下載源碼編譯的方式。/usr/local/php/etc/文件夾須要準備一個共享,這裏取上面官方的配置稍加修改,並共享出來供後續安裝使用:html

[]:~/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

測試shell到Dockerfile的代碼mysql

#測試
#swoole 須要對話參數,因此自定義安裝
RUN -ex && \
    mkdir -p ~/build && \
    cd ~/build && \
    rm -rf ./swoole-src && \
    mkdir tmp && \
    wget -O ./tmp/swoole.tar.gz https://github.com/swoole/swoole-src/archive/master.tar.gz && \
    tar zxvf ./tmp/swoole.tar.gz && \
    mv swoole-src* swoole-src && \
    cd swoole-src && \
    /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 -ex && \
    wget https://pecl.php.net/get/inotify-2.0.0.tgz && \
    tar -zxf inotify-2.0.0.tgz && \
    cd inotify-2.0.0 && \
    /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 -ex && \
    wget -O redis-4.3.0.tgz https://pecl.php.net/get/redis-4.3.0.tgz && \
    tar -zxf redis-4.3.0.tgz && \
    cd redis-4.3.0 && \
    /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 -ex && \
    wget "http://nchc.dl.sourceforge.net/project/libuuid/libuuid-1.0.3.tar.gz" && \
    tar -zxf libuuid-1.0.3.tar.gz && \
    cd libuuid-1.0.3 && \
    ./configure --prefix=/usr && \
    make && make install && \
    cd ../ && rm -rf libuuid* && \
    \
    wget http://pecl.php.net/get/uuid-1.0.4.tgz && \
    tar zxf uuid-1.0.4.tgz && \
    cd uuid-1.0.4 && \
    /usr/local/php/bin/phpize && \
    ./configure --with-php-config=/usr/local/php/bin/php-config && \
    make && make install && \
    cd ../ && rm -rf uuid*


#編譯使用,運行時共享 -v /your_real_path/ /usr/local/php/etc/
COPY config /usr/local/php/etc
VOLUME ["/usr/local/php/etc","/var/www/html"]

RUN -ex \
    && apk add libmemcached && /usr/local/php/bin/pecl channel-update pecl.php.net \
    && /usr/local/php/bin/pecl install igbinary event memcached \
    && rm -rf /tmp/pear ~/.pearrc ~/build \
    && apk del $PHPIZE_DEPS $PHPIZE_DEVS \
    && /usr/local/php/bin/php -m

三、合併,再添加linux

CMD ["/usr/local/php/etc/start.sh"]
相關文章
相關標籤/搜索