hyperf Dockerfile 配置及 PHP 第三方擴充套件的安裝

Komorebi_PHP發表於2020-03-12

安裝擴充套件

安裝部分移步至Hyperf,這裡主要講解的是php擴充套件的安裝和在docker Dockerfile的配置

目錄介紹

基於hyperf官方Dockerfile增加的一些配置

  1. entrypoint.sh,inst.sh,tracker.sh,deploy_env.sh

    swoole_tracker官方執行指令碼

  2. swoole-tracker.ini

    swoole_tracker.ini是swoole_tracker的一些配置選項

  3. xlswriter.tar.gz

    xlswriter的擴充套件包

  4. swoole.tar.gz

    swoole的擴充套件包

  5. 其他的一些擴充套件包也可以放在這個目錄裡

Dockerfile

# hyperf-cloud/hyperf:7.3-alpine-cli
#
# @link     https://www.hyperf.io
# @document https://doc.hyperf.io
# @contact  group@hyperf.io
# @license  https://github.com/hyperf-cloud/hyperf/blob/master/LICENSE

FROM hyperf/hyperf:7.3-alpine-v3.9-base

LABEL maintainer="Hyperf Developers <group@hyperf.io>" version="1.0" license="MIT"

RUN sed -i "s/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g" /etc/apk/repositories


ARG SWOOLE_VERSION

ENV SWOOLE_VERSION=${SWOOLE_VERSION:-"4.4.16"} \
    TIMEZONE=${timezone:-"Asia/Shanghai"} \
    ## 自定義配置就行
    COMPOSER_VERSION=${COMPOSER_VERSION:-"1.9.3"} \
    PHPIZE_DEPS="autoconf dpkg-dev dpkg file g++ gcc libc-dev make php7-dev php7-pear pkgconf re2c pcre-dev pcre2-dev zlib-dev libtool automake"

RUN set -ex \
    && apk update \
    # for swoole extension libaio linux-headers
    && apk add --no-cache libstdc++ openssl git bash \
    && apk add --no-cache --virtual .build-deps $PHPIZE_DEPS libaio-dev openssl-dev


COPY .build/swoole.tar.gz /tmp
COPY .build/xlswriter.tar.gz /tmp
RUN cd /tmp \
    && ls -alh \
    # php extension:swoole
    && cd /tmp \
    && mkdir -p swoole \
    && tar -xf swoole.tar.gz -C swoole --strip-components=1 \
    && ln -s /usr/bin/phpize7 /usr/local/bin/phpize \
    && ln -s /usr/bin/php-config7 /usr/local/bin/php-config \
    && ( \
        cd swoole \
        && phpize \
        && ./configure --enable-mysqlnd --enable-openssl --enable-http2 \
        && make -s -j$(nproc) && make install \
    ) \
    && echo "extension=swoole.so" > /etc/php7/conf.d/50_swoole.ini \
    && echo "swoole.use_shortname='Off'" >> /etc/php7/conf.d/50_swoole.ini \
    # xlswriter
    && cd /tmp \
    && mkdir -p xlswriter \
    && tar -xf xlswriter.tar.gz -C xlswriter --strip-components=1 \
    && ( \
        cd xlswriter \
        && phpize \
        && ./configure --enable-reader \
        && make -s -j$(nproc) && make install \
    ) \
    && echo "extension=xlswriter.so" > /etc/php7/conf.d/50_xlswriter.ini \
    # clear
    && php -v \
    && php -m \
    && php --ri swoole

COPY .build/tracker.sh /tmp

RUN chmod +x /tmp/tracker.sh && \
    cd /tmp/ && \
    ./tracker.sh && \
    rm /tmp/tracker.sh
RUN  echo "[swoole_tracker]" >> /etc/php7/conf.d/50_swoole.ini
RUN  echo "extension=/opt/swoole_tracker.so" >> /etc/php7/conf.d/50_swoole.ini
RUN  echo "apm.enable=1           #開啟總開關" >> /etc/php7/conf.d/50_swoole.ini
RUN  echo "apm.sampling_rate=100  #取樣率 例如:100%" >> /etc/php7/conf.d/50_swoole.ini
RUN  echo "apm.enable_memcheck=1  #開啟記憶體洩漏檢測 預設0 關閉狀態" >> /etc/php7/conf.d/50_swoole.ini
RUN cp /tmp/swoole-tracker/swoole_tracker73.so /opt/swoole_tracker.so
 #  ---------- some config ----------
RUN  cd /etc/php7 \
    # - config PHP
    && { \
        echo "upload_max_filesize=100M"; \
        echo "post_max_size=108M"; \
        echo "memory_limit=1024M"; \
        echo "date.timezone=${TIMEZONE}"; \
    } | tee /etc/php7/conf.d/99-overrides.ini
# ----------- install composer -----------#
RUN cd /tmp \
    && wget https://github.com/composer/composer/releases/download/${COMPOSER_VERSION}/composer.phar \
    && chmod u+x composer.phar \
    && mkdir -p /usr/local/bin/composer \
    && cp composer.phar /usr/local/bin/composer
# ---------- clear works ----------
RUN apk del .build-deps \
    && rm -rf /var/cache/apk/* /tmp/* /usr/share/man /usr/local/bin/php* \
    && echo -e "\033[42;37m Build Completed :).\033[0m\n"
WORKDIR /www
VOLUME /www
CMD ["php","bin/hyperf.php","start"]

自定義擴充套件安裝

1. 首先本地下載好擴充套件包複製到.build目錄裡去

2.編輯dockerfile 增加 COPY .build/xxxx.tar.gz /tmp

3.例如安裝xlswriter  編輯dockerfile配置如下
    # xlswriter
      && cd /tmp \
      && mkdir -p xlswriter \
      && tar -xf xlswriter.tar.gz -C xlswriter --strip-components=1 \
      && ( \
        cd xlswriter \
        && phpize \
        && ./configure --enable-reader \
        && make -s -j$(nproc) && make install \
      ) \
     && echo "extension=xlswriter.so" > /etc/php7/conf.d/50_xlswriter.ini \
4.docker build -t hyperf:v1.0 .  構建映象

5.docker run -it -p 127.0.0.1:9504:9504 -v $(pwd):/www --name heyperf_name(容器名稱) hyperf:v1.0 /bin/bash 使用映象"hyperf:v1.0" 以互動模式啟動一個容器並命名為”heyperf_name”,在容器內執行”/bin/bash”,而且繫結容器的9504,並且對映到宿主機127.0.0.19504埠上.-v $(pwd):/www對映目錄到容器目錄內裡

6.登入容器: docker exec -it heyperf_name /bin/bash(注意我是mac環境,window或者linux請自行百度)
本作品採用《CC 協議》,轉載必須註明作者和本文連結

相關文章