PHP教程

基于官方镜像定制php-fpm容器

本文主要是介绍基于官方镜像定制php-fpm容器,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

PHP官方docker镜像分类

  • php:-cli

  • php:-apache

  • php:-fpm

  • php:-alpine

制定适合生产环境的镜像

为何要定制

官方镜像缺少常见扩展 以php:8.0.8-fpm-alpine3.13为例

定制步骤

  • 新建目录
    以/root/docker为例
    mkdir php/conf.d -p
    新建Dockerfile 输入
FROM php:8.0.8-fpm-alpine3.13

LABEL maintainer="leo leo2768@163.com"

# 设置apk源为国内镜像源
RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories
COPY ./conf.d/ $PHP_INI_DIR/conf.d/

ENV TZ "Asia/Shanghai"
ENV TERM xterm
# 默认关闭opcode
ENV OPCODE 0

# 添加用户
RUN addgroup -g 1000 -S www && adduser -s /sbin/nologin -S -D -u 1000 -G www www
# 指定扩展版本
ENV PHPREDIS_VERSION 5.3.4
ENV PHPXDEBUG_VERSION 2.6.0
ENV PHPZIP_VERSION 1.19.3
ENV PHPSWOOLE_VERSION 4.6.7
ENV PHPYAF_VERSION 3.3.2
ENV PHPAMQP_VERSION 1.11.0beta
ENV PHPMONGODB_VERSION 1.9.1

# PHPIZE_DEPS 包含 gcc g++ 等编译辅助类库,完成编译后删除
RUN apk add --no-cache $PHPIZE_DEPS \
    && apk add --no-cache libstdc++ libzip-dev vim\
    && apk add rabbitmq-c-dev \
    && apk update \
    && pecl install redis-$PHPREDIS_VERSION \
    && pecl install zip-$PHPZIP_VERSION \
    && pecl install yaf-$PHPYAF_VERSION \
    && pecl install swoole-$PHPSWOOLE_VERSION \
    && pecl install mongodb-$PHPMONGODB_VERSION \
    && pecl install amqp-$PHPAMQP_VERSION \
    && docker-php-ext-enable redis zip yaf swoole mongodb amqp \
    && apk del $PHPIZE_DEPS

# 安装扩展
RUN apk add --no-cache \
       freetype \
       libpng \
       libjpeg-turbo \
       freetype-dev \
       libpng-dev \
       jpeg-dev \
       libjpeg \
       libjpeg-turbo-dev \
       libwebp \
       libwebp-dev \
    && NUMPROC=$(grep -c ^processor /proc/cpuinfo 2>/dev/null || 1) \
    && docker-php-ext-install -j${NUMPROC} gd \
    && docker-php-ext-install -j${NUMPROC} pdo_mysql \
    && docker-php-ext-install -j${NUMPROC} opcache \
    && docker-php-ext-install -j${NUMPROC} bcmath \ 
    && docker-php-ext-install -j${NUMPROC} mysqli
# 拷贝配置文件
COPY  www.conf /usr/local/etc/php-fpm.d/www.conf

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

新建www.conf 输入

[www]
user = www
group = www
listen = 0.0.0.0:9000
pm = dynamic
pm.max_children = 6
pm.start_servers = 4
pm.min_spare_servers = 1
pm.max_spare_servers = 5

在conf.d下新建date.ini 输入

date.timezone = Asia/Shanghai

在conf.d下新建opcode.ini 输入

opcache.enable= 0
enable_clopcache.enable_cli=1
opcache.revalidate_freq=60
opcache.max_accelerated_files=100000
opcache.validate_timestamps=1

新建build.sh 输入

#/bin/bash
docker build -t php8-swoole-yaf-redis-amqp:latest .

目录结构如下:

.
|-- build.sh
|-- conf.d
|   |-- date.ini
|   `-- opcode.ini
|-- Dockerfile
`-- www.conf

执行sh build.sh
结果:

...
more information, such as the ld(1) and ld.so(8) manual pages.
----------------------------------------------------------------------

Build complete.
Don't forget to run 'make test'.

Installing shared extensions:     /usr/local/lib/php/extensions/no-debug-non-zts-20200930/
Installing header files:          /usr/local/include/php/
find . -name \*.gcno -o -name \*.gcda | xargs rm -f
find . -name \*.lo -o -name \*.o | xargs rm -f
find . -name \*.la -o -name \*.a | xargs rm -f
find . -name \*.so | xargs rm -f
find . -name .libs -a -type d|xargs rm -rf
rm -f libphp.la      modules/* libs/*
rm -f ext/opcache/jit/zend_jit_x86.c
(1/22) Purging .phpize-deps (20210712.084651)
(2/22) Purging autoconf (2.69-r3)
(3/22) Purging m4 (1.4.18-r2)
(4/22) Purging dpkg-dev (1.20.6-r0)
(5/22) Purging perl (5.32.0-r0)
(6/22) Purging dpkg (1.20.6-r0)
(7/22) Purging file (5.39-r0)
(8/22) Purging g++ (10.2.1_pre1-r3)
(9/22) Purging gcc (10.2.1_pre1-r3)
(10/22) Purging binutils (2.35.2-r1)
(11/22) Purging libatomic (10.2.1_pre1-r3)
(12/22) Purging libgomp (10.2.1_pre1-r3)
(13/22) Purging libgphobos (10.2.1_pre1-r3)
(14/22) Purging libc-dev (0.7.2-r3)
(15/22) Purging musl-dev (1.2.2-r1)
(16/22) Purging make (4.3-r0)
(17/22) Purging re2c (1.3-r1)
(18/22) Purging libmagic (5.39-r0)
(19/22) Purging mpc1 (1.2.0-r0)
(20/22) Purging mpfr4 (4.1.0-r0)
(21/22) Purging isl22 (0.22-r0)
(22/22) Purging gmp (6.2.1-r0)
Executing busybox-1.32.1-r6.trigger
OK: 41 MiB in 59 packages
Removing intermediate container 8f12e0d4fbaf
 ---> 49e4817125f0
Step 18/19 : COPY  www.conf /usr/local/etc/php-fpm.d/www.conf
 ---> 89f47472322f
Step 19/19 : RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"
 ---> Running in f8a1dd909182
Removing intermediate container f8a1dd909182
 ---> d108fa70a68f
Successfully built d108fa70a68f
Successfully tagged php8-swoole-yaf-redis-amqp:latest

进入容器查看

这篇关于基于官方镜像定制php-fpm容器的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!