制作 PHP 7 + Nginx 的基础镜像,要求能够通过环境变量修改 PHP 和 Nginx 的部分常用配置。并且支持自定义启动脚本。
Dockerfile
# 3.10 没有 php7-mongodb包,继续用3.8
FROM alpine:3.8
ENV TIMEZONE Asia/Shanghai
RUN mkdir -p /home/wwwroot/default && \
mkdir -p /run/nginx && \
mkdir /home/nobody && chown -R nobody.nobody /home/nobody && \
sed -i 's/dl-cdn.alpinelinux.org/mirrors.aliyun.com/g' /etc/apk/repositories && \
sed -ri 's#^(nobody:.*)?:/:(.*)#\1:/home/nobody:\2#g' /etc/passwd
RUN apk add --no-cache tzdata && \
cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \
echo "${TIMEZONE}" > /etc/timezone
RUN apk add --no-cache dumb-init nginx php7 php7-fpm php7-common php7-gd \
php7-json php7-curl php7-mbstring php7-iconv php7-opcache \
php7-bcmath php7-ctype php7-xmlreader php7-xmlwriter php7-session \
php7-sockets php7-gettext php7-ldap php7-mongodb \
php7-mysqli php7-mysqlnd php7-pdo_mysql && \
sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" /etc/php7/php-fpm.conf && \
sed -i "s|;date.timezone =.*|date.timezone = ${TIMEZONE}|" /etc/php7/php.ini
COPY conf/default.conf /etc/nginx/conf.d/
COPY conf/nginx.conf /etc/nginx/
# 日志输入到标准输出
RUN ln -sf /dev/stdout /var/log/nginx/access.log && \
ln -sf /dev/stderr /var/log/nginx/error.log && \
ln -sf /dev/stderr /var/log/php7/error.log
COPY init.sh /
RUN chmod +x /init.sh
ENTRYPOINT ["/usr/bin/dumb-init", "-v", "--rewrite", "15:3"]
CMD ["/init.sh"]
--rewrite 15:3
意思是把 SIGTERM
重写为 SIGQUIT
,因为对 Nginx 来说,SIGQUIT
是 优雅退出。
TERM, INT fast shutdown
QUIT graceful shutdown
HUP changing configuration, keeping up with a changed time zone (only for FreeBSD and Linux), starting new worker processes with a new configuration, graceful shutdown of old worker processes
USR1 re-opening log files
USR2 upgrading an executable file
WINCH graceful shutdown of worker processes
Nginx default.conf
:
server {
listen 80;
server_name *.yourserver.com;
index index.html index.htm index.php;
root /home/wwwroot/default;
location ~ .php(/|$) {
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
include fastcgi_params;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param PATH_INFO $fastcgi_script_name;
}
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
{
expires 30d;
}
location ~ .*\.(js|css)?$
{
expires 12h;
}
location ~ /\.
{
deny all;
}
}
自定义配置
在 init.sh
处理。
#!/bin/sh
function getEnv() {
if [ "$1"x == ""x ];then
echo $2
else
echo $1
fi
}
PHP_FPM_MODE=`getEnv "$PHP_FPM_MODE" "dynamic"`
PHP_FPM_MAX_CHILDREN=`getEnv "$PHP_FPM_MAX_CHILDREN" "100"`
PHP_FPM_START_SERVERS=`getEnv "$PHP_FPM_START_SERVERS" "30"`
PHP_FPM_MIN_SPARE_SERVERS=`getEnv "$PHP_FPM_MIN_SPARE_SERVERS" "10"`
PHP_FPM_MAX_SPARE_SERVERS=`getEnv "$PHP_FPM_MAX_SPARE_SERVERS" "50"`
PHP_FPM_MAX_REQUESTS=`getEnv "$PHP_FPM_MAX_REQUESTS" "100000"`
NGINX_WORKER_CONNECTIONS=`getEnv "$NGINX_WORKER_CONNECTIONS" "65535"`
NGINX_WORKER_RLIMIT_NOFILE=`getEnv "$NGINX_WORKER_RLIMIT_NOFILE" "80000"`
NGINX_WORKER_PROCESSES=`getEnv "$NGINX_WORKER_PROCESSES" "auto"`
PHP_CONF="/etc/php7/php-fpm.d/www.conf"
NGINX_CONF="/etc/nginx/nginx.conf"
sed -i "s/^pm = .*/pm = $PHP_FPM_MODE/g" $PHP_CONF
sed -i "s/^pm.max_children = .*/pm.max_children = $PHP_FPM_MAX_CHILDREN/g" $PHP_CONF
sed -i "s/^pm.start_servers = .*/pm.start_servers = $PHP_FPM_START_SERVERS/g" $PHP_CONF
sed -i "s/^pm.min_spare_servers = .*/pm.min_spare_servers = $PHP_FPM_MIN_SPARE_SERVERS/g" $PHP_CONF
sed -i "s/^pm.max_spare_servers = .*/pm.max_spare_servers = $PHP_FPM_MAX_SPARE_SERVERS/g" $PHP_CONF
sed -i "s/^;pm.max_requests = .*/pm.max_requests = $PHP_FPM_MAX_REQUESTS/g" $PHP_CONF
sed -i "s/worker_rlimit_nofile .*/worker_rlimit_nofile $NGINX_WORKER_RLIMIT_NOFILE;/g" $NGINX_CONF
sed -i "s/worker_connections .*/worker_connections $NGINX_WORKER_CONNECTIONS;/g" $NGINX_CONF
sed -i "s/worker_processes .*/worker_processes $NGINX_WORKER_PROCESSES;/g" $NGINX_CONF
# catch_workers_output = yes 日志输出到stdout stderr
echo "catch_workers_output = yes" >> $PHP_CONF
chown -R nobody.nobody /home/wwwroot/default/
# PHP读取到环境变量
env |grep -v "=$" | grep "=" | sed -r "s/([a-zA-Z0-9_.]+)=(.*)/env[\1]='\2'/" |grep "^env\[" >> $PHP_CONF
crond
php-fpm7 -D
exec nginx
自定义启动脚本
init.sh
中:
# 自定义脚本
if [ -f /app.sh ];then
source /app.sh
fi
crond
php-fpm7 -D
exec nginx
然后使用该镜像的业务可以在 app.sh
中加入自定义脚本。
参考资料
1. http://nginx.org/en/docs/control.html
发表回复