我花了大概一周的时间进行了各种踩坑及实验,最终得出了一份可以使用的dockerfile及compose
内含如下支持
- php7
- mysql_pdo
- postgre_pdo
- phpredis
- swoole(可选,如应用swoole,dockerfile及nginx的配置会有所变化)
dockerfile分为两部分,一部分为php服务,一部分为nginx(swoole下可选)
先贴代码吧
### php7 ###
FROM php:7.2-rc-fpm-alpine3.6 ENV TIMEZONE Asia/Shanghai ENV PHP_MEMORY_LIMIT 512M ENV MAX_UPLOAD 50M ENV PHP_MAX_FILE_UPLOAD 200 ENV PHP_MAX_POST 100M ## swoole版本,如需安装swoole则取消注释 #ENV PHP_EXT_SWOOLE=swoole-2.0.6 ENV PHP_REDIS=3.1.4 #基础依赖 RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \ apk update && \ apk add tzdata curl && \ cp /usr/share/zoneinfo/${TIMEZONE} /etc/localtime && \ echo "${TIMEZONE}" > /etc/timezone && \ apk --update --repository=http://dl-4.alpinelinux.org/alpine/edge/testing add \ php7-common php7-intl php7-gd php7-mcrypt php7-openssl \ php7-gmp php7-json php7-dom php7-pdo php7-zip \ php7-zlib php7-mysqli php7-bcmath php7-pdo_mysql php7-pgsql \ php7-pdo_pgsql php7-gettext php7-xmlreaderhp7-xmlrpc \ php7-bz2 php7-iconv php7-curl php7-ctype php7-fpm \ php7-mbstring php7-session php7-phar curl curl-dev postgresql-dev \ ## 如果使用swoole 需要取消下行注释 # hiredis-dev libmcrypt-dev gmp-dev icu-dev linux-headers musl --virtual .phpize-deps $PHPIZE_DEPS \ tzdata && \ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \ php composer-setup.php && \ php -r "unlink('composer-setup.php');" && \ mv composer.phar /usr/local/bin/composer && \ composer self-update && \ sed -i -e "s/;daemonize\s*=\s*yes/daemonize = no/g" /etc/php7/php-fpm.conf && \ sed -i -e "s/listen\s*=\s*127.0.0.1:9000/listen = 9000/g" /etc/php7/php-fpm.d/www.conf && \ sed -i "s|;date.timezone =.*|date.timezone = ${TIMEZONE}|" /etc/php7/php.ini && \ sed -i "s|memory_limit =.*|memory_limit = ${PHP_MEMORY_LIMIT}|" /etc/php7/php.ini && \ sed -i "s|upload_max_filesize =.*|upload_max_filesize = ${MAX_UPLOAD}|" /etc/php7/php.ini && \ sed -i "s|max_file_uploads =.*|max_file_uploads = ${PHP_MAX_FILE_UPLOAD}|" /etc/php7/php.ini && \ sed -i "s|post_max_size =.*|max_file_uploads = ${PHP_MAX_POST}|" /etc/php7/php.ini && \ sed -i "s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php7/php.ini && \ sed -i "s/;extension=php_pgsql.dll/extension=php_pgsql.dll/" /etc/php7/php.ini && \ sed -i "s/;extension=php_pdo_pgsql.dll/extension=php_pdo_pgsql.dll/" /etc/php7/php.ini && \ mkdir -p /usr/src/php/ext/redis && \ curl -L https://github.com/phpredis/phpredis/archive/$PHP_REDIS.tar.gz | tar xvz -C /usr/src/php/ext/redis --strip 1 && \ echo 'redis' >> /usr/src/php-available-exts && \ docker-php-ext-install redis pgsql pdo pdo_mysql pdo_pgsql && \ rm -rf /var/cache/apk/* # 如需安装swoole,取消以下注释 #RUN \ # cd /tmp \ # && pecl download $PHP_EXT_SWOOLE \ # && mkdir -p /tmp/$PHP_EXT_SWOOLE \ # && tar -xf ${PHP_EXT_SWOOLE}.tgz -C /tmp/$PHP_EXT_SWOOLE --strip-components=1 \ # && docker-php-ext-configure /tmp/$PHP_EXT_SWOOLE --enable-async-redis --enable-openssl --enable-sockets=/usr/local/include/php/ext/sockets \ # && docker-php-ext-install /tmp/$PHP_EXT_SWOOLE \ # && rm -rf /tmp/${PHP_EXT_SWOOLE}* WORKDIR /www # 放入自己需要的代码 #COPY . /www # 安装composer依赖 #RUN composer install # php-fpm使用以下配置 EXPOSE 9000 CMD ["php-fpm"] # swoole 使用以下配置 EXPOSE 9501 # 启动swoole server CMD ["php","src/server","start"]
Salin selepas log masuk
## swoole可以不依赖nginx 所以我单独贴出php-fpm的nginx
nginx-dockerfile:
from nginx:1.13.6-alpine ENV TIME_ZONE Asiz/Shanghai RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.ustc.edu.cn/g' /etc/apk/repositories && \ apk update && \ apk add --no-cache tzdata && \ echo "${TIME_ZONE}" > /etc/timezone && \ ln -sf /usr/share/zoneinfo/${TIME_ZONE} /etc/localtime COPY default.conf /etc/nginx/conf.d EXPOSE 80 STOPSIGNAL SIGTERM CMD ["nginx", "-g", "daemon off;"]
Salin selepas log masuk
nginx_php-fpm:default.conf:
server { listen 80; server_name *.yourserver.com; location / { autoindex on; if ($request_filename !~* /(index\.php|assets|uploads|phpinfo\.php)) { rewrite ^/(.*)$ /index.php/$1 last; } } location ~ .php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; fastcgi_pass php-fpm:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /www$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; include fastcgi_params; } }
Salin selepas log masuk
compose:
* swoole的编排模板很容易写 我就不在这里贴出了
version: '2' services: php-fpm: image: "php-fpm" restart: always #日志系统,可不写 #logging: # driver: fluentd # options: # fluentd-address: "logaddr:24224" # tag: "docker/{{.Name}}/{{.ID}}/{{.ImageName}}" php-nginx: image: "php-nginx" depends_on: #这几个\是转义字符 但是不知道为什么就显示出来了,使用的时候自己去掉一下 \- php-fpm ports: \- "80:80" links: \- php-fpm restart: always #日志系统,可不写 #logging: # driver: fluentd # options: # fluentd-address: "logaddr:24224" # tag: "docker/{{.Name}}/{{.ID}}/{{.ImageName}}"
Salin selepas log masuk
我所做的是对php及nginx进行解耦
如果集群内有多个php服务需要互相访问,
因为compose不允许两个服务互相link
所以需要创建一个network在network中借助nginx进行互相访问
但是目前只有v3版的compose支持本功能,很多旧集群都无法正常支持本功能,所以我就不贴出代码了
编辑完成后,即可通过80端口访问index.php了