這篇文章主要介紹“如何構(gòu)建一個(gè)php7-alpine的docker鏡像”,在日常操作中,相信很多人在如何構(gòu)建一個(gè)php7-alpine的docker鏡像問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”如何構(gòu)建一個(gè)php7-alpine的docker鏡像”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
創(chuàng)新互聯(lián)公司自成立以來,一直致力于為企業(yè)提供從網(wǎng)站策劃、網(wǎng)站設(shè)計(jì)、做網(wǎng)站、網(wǎng)站設(shè)計(jì)、電子商務(wù)、網(wǎng)站推廣、網(wǎng)站優(yōu)化到為企業(yè)提供個(gè)性化軟件開發(fā)等基于互聯(lián)網(wǎng)的全面整合營銷服務(wù)。公司擁有豐富的網(wǎng)站建設(shè)和互聯(lián)網(wǎng)應(yīng)用系統(tǒng)開發(fā)管理經(jīng)驗(yàn)、成熟的應(yīng)用系統(tǒng)解決方案、優(yōu)秀的網(wǎng)站開發(fā)工程師團(tuán)隊(duì)及專業(yè)的網(wǎng)站設(shè)計(jì)師團(tuán)隊(duì)。
php7
MySQL_pdo
postgre_pdo
phpredis
swoole(可選,如應(yīng)用swoole,dockerfile及nginx的配置會有所變化)
dockerfile分為兩部分,一部分為php服務(wù),一部分為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 #基礎(chǔ)依賴 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 所以我單獨(dú)貼出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 #日志系統(tǒng),可不寫 #logging: # driver: fluentd # options: # fluentd-address: "logaddr:24224" # tag: "docker/{{.Name}}/{{.ID}}/{{.ImageName}}" php-nginx: image: "php-nginx" depends_on: #這幾個(gè)\是轉(zhuǎn)義字符 但是不知道為什么就顯示出來了,使用的時(shí)候自己去掉一下 \- php-fpm ports: \- "80:80" links: \- php-fpm restart: always #日志系統(tǒng),可不寫 #logging: # driver: fluentd # options: # fluentd-address: "logaddr:24224" # tag: "docker/{{.Name}}/{{.ID}}/{{.ImageName}}"
我所做的是對php及nginx進(jìn)行解耦
如果集群內(nèi)有多個(gè)php服務(wù)需要互相訪問,
因?yàn)閏ompose不允許兩個(gè)服務(wù)互相l(xiāng)ink
所以需要?jiǎng)?chuàng)建一個(gè)network在network中借助nginx進(jìn)行互相訪問
但是目前只有v3版的compose支持本功能,很多舊集群都無法正常支持本功能,所以我就不貼出代碼了
編輯完成后,即可通過80端口訪問index.php了
到此,關(guān)于“如何構(gòu)建一個(gè)php7-alpine的docker鏡像”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!