这篇文章主要介绍“如何构建一个php7-alpine的docker镜像”,在日常操作中,相信很多人在如何构建一个php7-alpine的docker镜像问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何构建一个php7-alpine的docker镜像”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!
创新互联公司自成立以来,一直致力于为企业提供从网站策划、网站设计、做网站、网站设计、电子商务、网站推广、网站优化到为企业提供个性化软件开发等基于互联网的全面整合营销服务。公司拥有丰富的网站建设和互联网应用系统开发管理经验、成熟的应用系统解决方案、优秀的网站开发工程师团队及专业的网站设计师团队。
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"]
## 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;"]
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; } }
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}}"
我所做的是对php及nginx进行解耦
如果集群内有多个php服务需要互相访问,
因为compose不允许两个服务互相link
所以需要创建一个network在network中借助nginx进行互相访问
但是目前只有v3版的compose支持本功能,很多旧集群都无法正常支持本功能,所以我就不贴出代码了
编辑完成后,即可通过80端口访问index.php了
到此,关于“如何构建一个php7-alpine的docker镜像”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注创新互联网站,小编会继续努力为大家带来更多实用的文章!