构建PHP容器

文档说明:只记录关键地方;

PHP 有两种运行模式 php-fpm 和 php-cli

构建脚本 php-cli-alpine

#!/bin/sh
set -ux 

TIME=`date -u '+%Y%m%dT%H%M%SZ'`
VERSION="7.4-cli-alpine-"${TIME}
IMAGE="wenba100xie/php:${VERSION}"
export DOCKER_BUILDKIT=1

# 使用代理 需要传递两个参数
# sh build-docker.sh --proxy 1
PROXY_URL=${2:+'http://192.168.3.26:8015'}

docker build -t ${IMAGE} -f Dockerfile . --progress=plain --build-arg PROXY_URL=$PROXY_URL
docker push ${IMAGE}

构建之前查看基础镜像默认配置


docker run --rm  php:7.4-cli-alpine php -i | grep php.ini
docker run --rm  php:7.4-cli-alpine php -m 

构建php cli

FROM php:7.4-cli-alpine

RUN sed -i "s@dl-cdn.alpinelinux.org/@mirrors.tuna.tsinghua.edu.cn/@g" /etc/apk/repositories
RUN uname -a
ENV TZ=Etc/UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk update && apk add --no-cache   gnu-libiconv  ca-certificates postgresql-dev openssl openssl-dev libzip-dev curl curl-dev
RUN apk add  --no-cache \
      freetype \
      freetype-dev \
      jpeg \
      libpng \
      libwebp \
      libpng \
      libpng-dev \
      libjpeg-turbo \
      libjpeg-turbo-dev \
      gd-dev \
      zlib-dev \
      gmp \
      gmp-dev \
      glib \
      glib-dev \
      libzip-dev \
      postgresql-dev \
      tini

RUN apk add build-base autoconf  
#RUN addgroup -g 1000 -S nginx && adduser -u 1000 -S nginx -G nginx -s /sbin/nologin


RUN docker-php-source extract

WORKDIR /usr/src/php/ext

RUN docker-php-ext-install gd
RUN docker-php-ext-install iconv
RUN docker-php-ext-install curl
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install sockets
RUN docker-php-ext-install zip
RUN docker-php-ext-install bz2
RUN docker-php-ext-install gmp
RUN docker-php-ext-install bcmath

# openssl 需要特殊处理
WORKDIR /usr/src/php/ext/openssl
RUN ls -lh .
RUN cp config0.m4 config.m4
RUN docker-php-ext-install openssl

RUN docker-php-source delete

ARG PROXY_URL=''

ENV http_proxy=$PROXY_URL
ENV https_proxy=$PROXY_URL

# RUN apk add --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing gnu-libiconv
# ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

RUN pear config-set http_proxy $PROXY_URL
RUN pecl channel-update https://pecl.php.net/channel.xml
RUN pear update-channels
RUN pecl config-show



WORKDIR /

RUN pecl install redis
RUN pecl install mongodb

RUN apk add c-ares c-ares-dev
# RUN pecl install swoole
RUN pecl install --configureoptions 'enable-sockets="yes" enable-openssl="yes"  enable-mysqlnd="yes"  enable-swoole-curl="yes" enable-cares="yes"' swoole

# curl ssl 证书
# https://curl.haxx.se/docs/caextract.html curl ssl 证书下载网址

RUN curl -fsSL 'https://curl.haxx.se/ca/cacert.pem' -o /usr/local/php/cacert.pem

ENV http_proxy=""
ENV https_proxy=$http_proxy
RUN pear config-set http_proxy ""

# 启用扩展
RUN  docker-php-ext-enable redis
RUN  docker-php-ext-enable mongodb
RUN  docker-php-ext-enable swoole

# /usr/local/etc/php/php.ini

RUN curl -Lo /usr/local/bin/composer https://mirrors.aliyun.com/composer/composer.phar
RUN chmod a+x /usr/local/bin/composer


RUN rm -rf /var/cache/apk/* /tmp

WORKDIR /

# USER 1000:1000

构建脚本 php-fpm-alpine

#!/bin/sh
set -ux 

TIME=`date -u '+%Y%m%dT%H%M%SZ'`
VERSION="7.4-fpm-alpine-"${TIME}
IMAGE="wenba100xie/php:${VERSION}"
export DOCKER_BUILDKIT=1

# 使用代理 需要传递两个参数
# sh build-docker.sh --proxy 1
PROXY_URL=${2:+'http://192.168.3.26:8015'}

docker build -t ${IMAGE} -f Dockerfile . --progress=plain --build-arg PROXY_URL=$PROXY_URL
docker push ${IMAGE}

构建之前查看基础镜像默认配置


docker run --rm --name php-fpm   php:7.4-fpm-alpine
docker exec -i php-fpm   php -i | grep php.ini
docker exec -i php-fpm   php -m 

构建php php-fpm

FROM php:7.4-fpm-alpine

RUN sed -i "s@dl-cdn.alpinelinux.org/@mirrors.tuna.tsinghua.edu.cn/@g" /etc/apk/repositories
RUN uname -a
ENV TZ=Etc/UTC
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
RUN apk update && apk add --no-cache   gnu-libiconv  ca-certificates postgresql-dev openssl openssl-dev libzip-dev curl curl-dev
RUN apk add  --no-cache \
      freetype \
      freetype-dev \
      jpeg \
      libpng \
      libwebp \
      libpng \
      libpng-dev \
      libjpeg-turbo \
      libjpeg-turbo-dev \
      gd-dev \
      zlib-dev \
      gmp \
      gmp-dev \
      glib \
      glib-dev \
      libzip-dev \
      postgresql-dev \
      tini

RUN apk add build-base autoconf 
#RUN addgroup -g 1000 -S nginx && adduser -u 1000 -S nginx -G nginx -s /sbin/nologin

RUN docker-php-source extract

WORKDIR /usr/src/php/ext

RUN docker-php-ext-install gd
RUN docker-php-ext-install iconv
RUN docker-php-ext-install curl
RUN docker-php-ext-install pdo_mysql
RUN docker-php-ext-install sockets
RUN docker-php-ext-install zip
RUN docker-php-ext-install bz2
RUN docker-php-ext-install gmp
RUN docker-php-ext-install bcmath

# openssl 需要特殊处理
WORKDIR /usr/src/php/ext/openssl
RUN ls -lh .
RUN cp config0.m4 config.m4
RUN docker-php-ext-install openssl

RUN docker-php-source delete

ARG PROXY_URL=''

ENV http_proxy=$PROXY_URL
ENV https_proxy=$PROXY_URL

# RUN apk add --no-cache --repository http://dl-3.alpinelinux.org/alpine/edge/testing gnu-libiconv
# ENV LD_PRELOAD /usr/lib/preloadable_libiconv.so php

RUN pear config-set http_proxy $PROXY_URL
RUN pecl channel-update https://pecl.php.net/channel.xml
RUN pear update-channels &&  pecl config-show



WORKDIR /

RUN pecl install redis
RUN pecl install mongodb


# curl ssl 证书
# https://curl.haxx.se/docs/caextract.html curl ssl 证书下载网址

RUN curl -fsSL 'https://curl.haxx.se/ca/cacert.pem' -o /usr/local/php/cacert.pem

ENV http_proxy=""
ENV https_proxy=$http_proxy
RUN pear config-set http_proxy ""

# 启用扩展
RUN  docker-php-ext-enable redis
RUN  docker-php-ext-enable mongodb



# /usr/local/etc/php/php.ini


RUN rm -rf /var/cache/apk/* /tmp

WORKDIR /

# USER 1000:1000

获得容器内默认配置信息


container_id=$(docker create php:7.4-fpm-alpine)  # returns container ID
docker cp $container_id:/usr/local/etc/php/php.ini-production php.ini
docker cp $container_id:/usr/local/etc/php-fpm.conf  php-fpm.conf
docker cp $container_id:/usr/local/etc/php-fpm.d/www.conf www.conf
docker rm $container_id

参考文档

  1. hub docker php
  2. github docker php
  3. hub docker php
  4. hub docker nginx
  5. php-fig
  6. curl ssl 证书
  7. PHP四种设置回调函数的方式
  8. Unix 系统下的安装
  9. php docs
  10. php 所支持的时区列表
  11. 阿里云 Composer 全量镜像
  12. 最新的 Composer 版本
  13. PHP packagist.org
  14. PHP 搜扩展包
  15. symfony
  16. laravel/laravel
  17. Dockerfile
  18. Compose file
  19. Tini作为Docker容器的init进程,tini 解决僵尸进程
  20. 构建php的两种模式(php-fpm和php-cli)的运行环境
  21. nginx容器与php-fpm容器连接
  22. 快速安装swoole的办法

参考文档2

  1. debian packages
  2. alpine packages
  3. web搜索软件包或者应用程序的入口
posted @ 2022-10-31 03:25  jingjingxyk  阅读(275)  评论(0编辑  收藏  举报