基于最小Linux静态编译最新版php7.4.5+swoole4.5的docker镜像

最近发现之前基于centos编译的swoole环境随着功能的扩展越来越大,下决心编译一款轻量的docker包。

上镜像链接

基础镜像包

大小:171M
环境:php7.4.5 + swoole4.5.0 + redis5.2

docker pull registry.cn-hangzhou.aliyuncs.com/liyunlong/php7.4.5_swoole_4.5:latest

开启的模块

[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
hash
iconv
json
libxml
mbstring
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
SimpleXML
SPL
sqlite3
standard
swoole
tokenizer
xml
xmlreader
xmlwriter
zlib

swoole配置

/data/www # php --ri swoole

swoole

Swoole => enabled
Author => Swoole Team <[email protected]>
Version => 4.5.0RC1
Built => Apr 22 2020 13:39:39
coroutine => enabled
epoll => enabled
eventfd => enabled
signalfd => enabled
cpu_affinity => enabled
spinlock => enabled
rwlock => enabled
openssl => OpenSSL 1.1.1d  10 Sep 2019
http2 => enabled
zlib => 1.2.11
mutex_timedlock => enabled
pthread_barrier => enabled
futex => enabled
async_redis => enabled

Directive => Local Value => Master Value
swoole.enable_coroutine => On => On
swoole.enable_library => On => On
swoole.enable_preemptive_scheduler => Off => Off
swoole.display_errors => On => On
swoole.use_shortname => On => On
swoole.unixsock_buffer_size => 8388608 => 8388608

Dockerfile 下载附件包

docker build -t php_7.4.5_swoole4.5 ./   #开启打包之旅 因为某些软件包下载比较慢 我下载好放入附加包
FROM alpine:3.11
# dependencies required for running "phpize"
# these get automatically installed and removed by "docker-php-ext-*" (unless they're already installed)
ENV PHPIZE_DEPS \
		autoconf \
		dpkg-dev dpkg \
		file \
		g++ \
		gcc \
		libc-dev \
		make \
		pkgconf \
		re2c

ENV PHP_INI_DIR=/usr/local/etc/php PREL="perl-5.30.2" SWOOLE="swoole-src-4.5.0RC1" PHP_CFLAGS="-fstack-protector-strong -fpic -fpie -O2 -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64" PHP_CPPFLAGS="$PHP_CFLAGS" PHP_LDFLAGS="-Wl,-O1 -pie" PHP_SRC_FILE=/usr/src/php SOFT="/soft-tmp" OPEN_SSL="openssl-1.1.1g"

# persistent / runtime deps
COPY soft $SOFT # 需要的软件包看附件 

RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories; \
        apk add --no-cache \
        tzdata \
#        bash \
		ca-certificates \
		curl \
		openssl \
		openssl-dev \
		tar; \
# https://github.com/docker-library/php/issues/494
#		bash; \
# ensure www-data user exists
    set -eux; \
	addgroup -g 82 -S www-data; \
	adduser -u 82 -D -S -G www-data www-data; \
# 82 is the standard uid/gid for "www-data" in Alpine
# https://git.alpinelinux.org/aports/tree/main/apache2/apache2.pre-install?h=3.9-stable
# https://git.alpinelinux.org/aports/tree/main/lighttpd/lighttpd.pre-install?h=3.9-stable
# https://git.alpinelinux.org/aports/tree/main/nginx/nginx.pre-install?h=3.9-stable
    \
	mkdir -p "$PHP_INI_DIR/conf.d"; \
# allow running as an arbitrary user (https://github.com/docker-library/php/issues/743)
	[ ! -d /data/www ]; \
	mkdir -p /data/www; \
	chown www-data:www-data /data/www; \
	chmod 777 /data/www;\
	mv $SOFT/docker-php-ext-* $SOFT/docker-php-source /usr/local/bin/ ;

####
####

# Apply stack smash protection to functions using local buffers and alloca()
# Make PHP's main executable position-independent (improves ASLR security mechanism, and has no performance impact on x86_64)
# Enable optimization (-O2)
# Enable linker optimization (this sorts the hash buckets to improve cache locality, and is non-default)
# https://github.com/docker-library/php/issues/272
# -D_LARGEFILE_SOURCE and -D_FILE_OFFSET_BITS=64 (https://www.php.net/manual/en/intro.filesystem.php)

RUN set -eux; \
	apk add --no-cache --virtual .build-deps \
		$PHPIZE_DEPS \
		argon2-dev \
		coreutils \
		curl-dev \
		libedit-dev \
		libsodium-dev \
		libxml2-dev \
		linux-headers \
		oniguruma-dev \
		sqlite-dev \
	; \
	\
	export CFLAGS="$PHP_CFLAGS" \
		CPPFLAGS="$PHP_CPPFLAGS" \
		LDFLAGS="$PHP_LDFLAGS" \
	; \
	cd "${SOFT}/${PREL}" ;\
    ./Configure -des -Dprefix=$HOME/localperl;\
    make -j "$(nproc)" && make test && make install;\
    cd "${SOFT}/${OPEN_SSL}" ;\
    ./config --prefix=/usr/local/openssl ;\
    ./config -t && make -j "$(nproc)" && make install;\
    docker-php-source extract; \
	cd /usr/src/php; \
	gnuArch="$(dpkg-architecture --query DEB_BUILD_GNU_TYPE)"; \
	./buildconf --force; \
	./configure \
		--build="$gnuArch" \
		--with-config-file-path="$PHP_INI_DIR" \
		--with-config-file-scan-dir="$PHP_INI_DIR/conf.d" \
		\
# make sure invalid --configure-flags are fatal errors intead of just warnings
		--enable-option-checking=fatal \
		\
# https://github.com/docker-library/php/issues/439
		--with-mhash \
		\
# --enable-ftp is included here because ftp_ssl_connect() needs ftp to be compiled statically (see https://github.com/docker-library/php/issues/236)
#		--enable-ftp \
# --enable-mbstring is included here because otherwise there's no way to get pecl to use it properly (see https://github.com/docker-library/php/issues/195)
		--enable-mbstring \
		--enable-fileinfo \
		--enable-xml \
		--enable-cli \
		--disable-cgi \
		--disable-phpdbg \
		--enable-swoole=yes,static \
		--enable-openssl --enable-http2 --with-openssl-dir=/usr/local/openssl \
		--enable-redis=yes,static \
# --enable-mysqlnd is included here because it's harder to compile after the fact than extensions are (since it's a plugin for several extensions, not an extension in itself)
#		--enable-mysqlnd \
# https://wiki.php.net/rfc/argon2_password_hash (7.2+)
		--with-password-argon2 \
# https://wiki.php.net/rfc/libsodium
		--with-sodium=shared \
# always build against system sqlite3 (https://github.com/php/php-src/commit/6083a387a81dbbd66d6316a3a12a63f06d5f7109)
		--with-pdo-sqlite=/usr \
		--with-sqlite3=/usr \
		--with-curl \
		--with-libedit \
		--with-openssl \
		--with-zlib \
		--with-libxml \
		--without-pear \
		\
# in PHP 7.4+, the pecl/pear installers are officially deprecated (requiring an explicit "--with-pear") and will be removed in PHP 8+; see also https://github.com/docker-library/php/issues/846#issuecomment-505638494
# bundled pcre does not support JIT on s390x
# https://manpages.debian.org/stretch/libpcre3-dev/pcrejit.3.en.html#AVAILABILITY_OF_JIT_SUPPORT
		$(test "$gnuArch" = 's390x-linux-musl' && echo '--without-pcre-jit') \
		\
		${PHP_EXTRA_CONFIGURE_ARGS:-} \
	; \
	make -j "$(nproc)"; \
	find -type f -name '*.a' -delete; \
	make install; \
	find /usr/local/bin /usr/local/sbin -type f -perm +0111 -exec strip --strip-all '{}' + || true; \
	make clean; \
	\
# https://github.com/docker-library/php/issues/692 (copy default example "php.ini" files somewhere easily discoverable)
	cp -v php.ini-* "$PHP_INI_DIR/"; \
	\
	cd /; \
	docker-php-source delete; \
	\
	runDeps="$( \
		scanelf --needed --nobanner --format '%n#p' --recursive /usr/local \
			| tr ',' '\n' \
			| sort -u \
			| awk 'system("[ -e /usr/local/lib/" $1 " ]") == 0 { next } { print "so:" $1 }' \
	)"; \
	apk add --no-cache $runDeps; \
	\
#	( \
#        cd "${SOFT}/${SWOOLE}" \
#        && phpize \
#        && ./configure --enable-openssl --enable-http2 \
#        && make -j "$(nproc)" \
#        && make install \
#    ); \
#    docker-php-ext-enable swoole;\
	apk del --no-network .build-deps;\
	rm -rf $SOFT; \
	ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
    echo "Asia/Shanghai" > /etc/timezone;

基于基础镜像扩展的项目镜像(个人公司用)

大小:260M ,附件包暂未提供,需要的加我微信 : lyl332022596
增加扩展:+mongo1.8.0 +gd库 +高性能Excel库:xlswriter +Composer V1.10.5

docker pull registry.cn-hangzhou.aliyuncs.com/liyunlong/ajt-php:latest
[PHP Modules]
Core
ctype
curl
date
dom
fileinfo
filter
gd
hash
iconv
json
libxml
mbstring
mongodb
openssl
pcre
PDO
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
SimpleXML
SPL
sqlite3
standard
swoole
tokenizer
xlswriter
xml
xmlreader
xmlwriter
zip
zlib

搭建EasySwoole项目开发环境

docker-compose.yml

version: '3'
services:
  php:
    build:
      context: ./
      dockerfile: php/Dockerfile
    environment:
      APPNAME:
    networks:
      - ajt
    depends_on:
      - mysql
      - mongo
      - redis
    volumes:
      - ${WEB_DIR}:/data/www
      - ${BUILD_BASE_DIR}php/${ENTRY_POINT_FILE}:/data/${ENTRY_POINT_FILE}
    entrypoint: /data/${ENTRY_POINT_FILE}
    ports:
      - "9501:9501"
      - "9600:9600"
    links:
      - mysql
      - mongo
      - redis
    privileged: true
  redis:
    image: redis
    ports:
      - "6379:6379"
    networks:
      - ajt
    volumes:
      - ${REDIS_DATA_DIR}:/data/redis
  mysql:
    build:
      context: ./
      dockerfile: mysql/Dockerfile
    restart: always
    environment:
      MYSQL_ROOT_PASSWORD: 123456
    command:
      --default-authentication-plugin=mysql_native_password
      --character-set-server=utf8mb4
      --collation-server=utf8mb4_general_ci
      --explicit_defaults_for_timestamp=true
      --lower_case_table_names=1
      --max_allowed_packet=128M;
    ports:
      - "3306:3306"
    networks:
      - ajt
    volumes:
      - ${MYSQL_DATA_BACKUP}:/data/backup
      - ${MYSQL_DATA_DIR}:/var/lib/mysql
      - ${MYSQL_CONFIG_DIR}:/etc/mysql
  mongo:
    image: mongo:4.2.1
    ports:
      - "27017:27017"
    networks:
      - ajt
    volumes:
      - ${MONGO_DATA_DIR}:/data/db
      - ${MONGO_DATA_TMP_DIR}:/data/mongo_data_tmp
  nginx:
    image: nginx
    ports:
      - "80:80"
      - "443:443"
    networks:
      - ajt
    depends_on:
      - php
    volumes:
      - ${WEB_DIR}:/data/www
      - ${ERP_ADMIN_DIR}:/data/erp
      - ${BUILD_BASE_DIR}nginx/conf/nginx.conf:/etc/nginx/nginx.conf
      - ${BUILD_BASE_DIR}nginx/conf/conf.d:/etc/nginx/conf.d
      - ${BUILD_BASE_DIR}nginx/logs:/var/log/nginx
      - ${BUILD_BASE_DIR}nginx/cert:/etc/nginx/cert
    links:
      - php
networks:
  ajt:
    driver: bridge

php的Dockerfile

FROM registry.cn-hangzhou.aliyuncs.com/liyunlong/ajt-php:latest
WORKDIR /data/www

最后:基于最小Linux镜像会有很多坑希望能给跟我一样的追求精简的人少走坑!另外后期会继续优化基础镜像,也会随之php跟swoole的版本不断更新!

你可能感兴趣的:(php,Linux)