Table of Contents
内含如下支持
Home Backend Development PHP7 Finally got the docker image of php7-alpine

Finally got the docker image of php7-alpine

Sep 17, 2021 pm 04:39 PM
docker php7

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

## 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;"]
Copy after login

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;
    } 
}
Copy after login

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}}"
Copy after login

我所做的是对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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to update the image of docker How to update the image of docker Apr 15, 2025 pm 12:03 PM

The steps to update a Docker image are as follows: Pull the latest image tag New image Delete the old image for a specific tag (optional) Restart the container (if needed)

How to create a mirror in docker How to create a mirror in docker Apr 15, 2025 am 11:27 AM

Steps to create a Docker image: Write a Dockerfile that contains the build instructions. Build the image in the terminal, using the docker build command. Tag the image and assign names and tags using the docker tag command.

How to use docker desktop How to use docker desktop Apr 15, 2025 am 11:45 AM

How to use Docker Desktop? Docker Desktop is a tool for running Docker containers on local machines. The steps to use include: 1. Install Docker Desktop; 2. Start Docker Desktop; 3. Create Docker image (using Dockerfile); 4. Build Docker image (using docker build); 5. Run Docker container (using docker run).

How to read the docker version How to read the docker version Apr 15, 2025 am 11:51 AM

To get the Docker version, you can perform the following steps: Run the Docker command "docker --version" to view the client and server versions. For Mac or Windows, you can also view version information through the Version tab of the Docker Desktop GUI or the About Docker Desktop menu.

How to save docker image How to save docker image Apr 15, 2025 am 11:54 AM

To save the image in Docker, you can use the docker commit command to create a new image, containing the current state of the specified container, syntax: docker commit [Options] Container ID Image name. To save the image to the repository, you can use the docker push command, syntax: docker push image name [: tag]. To import saved images, you can use the docker pull command, syntax: docker pull image name [: tag].

How to copy files in docker to outside How to copy files in docker to outside Apr 15, 2025 pm 12:12 PM

Methods for copying files to external hosts in Docker: Use the docker cp command: Execute docker cp [Options] <Container Path> <Host Path>. Using data volumes: Create a directory on the host, and use the -v parameter to mount the directory into the container when creating the container to achieve bidirectional file synchronization.

How to check the name of the docker container How to check the name of the docker container Apr 15, 2025 pm 12:21 PM

You can query the Docker container name by following the steps: List all containers (docker ps). Filter the container list (using the grep command). Gets the container name (located in the "NAMES" column).

How to start containers by docker How to start containers by docker Apr 15, 2025 pm 12:27 PM

Docker container startup steps: Pull the container image: Run "docker pull [mirror name]". Create a container: Use "docker create [options] [mirror name] [commands and parameters]". Start the container: Execute "docker start [Container name or ID]". Check container status: Verify that the container is running with "docker ps".

See all articles