使用 alpine 镜像构建 php-fpm 镜像,添加 composer 和 php 扩展

dockerfile 文件如下

FROM alpine:3.12

ADD https://dl.bintray.com/php-alpine/key/php-alpine.rsa.pub /etc/apk/keys/php-alpine.rsa.pub

RUN apk --update-cache add ca-certificates && \
    echo "https://dl.bintray.com/php-alpine/v3.12/php-7.4" >> /etc/apk/repositories

RUN apk add --update-cache \
    php \
    php-fpm \
    php-bcmath \
    php-bz2 \
    php-ctype \
    php-curl \
    php-exif \
    php-gd \
    php-gettext \
    php-iconv \
    php-mbstring \
    php-session \
    php-phar \
    php-xml \
    php-xmlreader \
    # php-mcrypt \
    # php-fileinfo \
    php-openssl \
    php-opcache \
    php-pcntl \
    php-json \
    php-pdo \
    php-pdo_mysql \
    php-imagick \
    php-redis \
    php-zip \
    php-zlib

EXPOSE 9000

CMD php-fpm7 -F
  • 构建镜像
docker build -f ./dockerfile -t 192.168.1.66:5000/php7.4 ./

ps: 构建的此镜像不含有 composer,默认入口是 php-fpm,命令为 php7php-fpm7

  • 进入容器内部,添加 composerphp 命令,然后重新构建新的镜像
docker run -it --rm 192.168.1.66:5000/php7.4
cd usr/bin
cp php7 php
wget https://mirrors.aliyun.com/composer/composer.phar
chmod u+x composer.phar
mv composer.phar composer
  • 修改 php-fpm 相关配置
cd /etc/php7/php-fpm.d
  • 添加 docker.conf文件
[global]
error_log = /proc/self/fd/2

; https://github.com/docker-library/php/pull/725#issuecomment-443540114
log_limit = 8192

[www]
; if we send this to /proc/self/fd/1, it never appears
access.log = /proc/self/fd/2

clear_env = no

; Ensure worker stdout and stderr are sent to the main error log.
catch_workers_output = yes
decorate_workers_output = no
  • 添加 zz-docker.conf文件
[global]
daemonize = no

[www]
listen = 9000
  • 依据此容器重新构建镜像(当前容器不要退出,在开一个新的shell执行如下命令)
docker commit -m="composer" -a="sorry510" 容器id 192.168.1.66:5000/php7.4:composer
  • 使用此容器当作 php-fpm 时,注意 php-fpm 必须以前台方式运行
FROM 192.168.1.66:5000/php7.4:composer

EXPOSE 9000

CMD php-fpm7 -F

你可能感兴趣的:(使用 alpine 镜像构建 php-fpm 镜像,添加 composer 和 php 扩展)