search
HomeBackend DevelopmentPHP7Finally got the docker image of php7-alpine

我花了大概一周的时间进行了各种踩坑及实验,最终得出了一份可以使用的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"]

## 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教程

The above is the detailed content of Finally got the docker image of php7-alpine. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:segmentfault. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft