在一个dockerfile中结合PHP-fpm和nginx

我需要将php-fpm与nginx结合在一个dockerfile中进行生产部署。

所以更好的方法是:

(1)使用php:7.1.8-fpm启动dockerfile,然后在其之上安装nginx映像层?

(2)还是建议使用Nginx映像,然后使用apt-get安装php-fpm?

PS:我没有用于生产部署的docker-compose构建选项。 在我的开发环境中,我已经使用docker-compose并从两个映像轻松构建多容器应用程序。 我们的组织开发人员不支持针对产品环境的基于docker-compose的部署。

You should deploy two container, one with fpm, the other with nginx, and you should link them. Even though you can use supervisor in order to monitore multiple processes within the same container, Docker philosophy is to have one process per container.

Something like:

docker run --name php -v ./code:/code php:7-fpm
docker run --name nginx -v ./code:/code -v site.conf:/etc/nginx/conf.d/site.conf --link php nginx:latest

With site.conf with

server {
    index index.php index.html;
    server_name php-docker.local;
    error_log  /var/log/nginx/error.log;
    access_log /var/log/nginx/access.log;
    root /code;

    location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass php:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
}

(Shamefully inspired by http://geekyplatypus.com/dockerise-your-php-application-with-nginx-and-php7-fpm/)

Nginx installation is much easier than PHP so it should be easier for you to install Nginx into ready-to-use official PHP image. Here is the example Dockerfile showing how your goal can be reached with example of installing few PHP extensions:

FROM php:7.2-fpm

RUN apt-get update -y \
    && apt-get install -y nginx

# PHP_CPPFLAGS are used by the docker-php-ext-* scripts
ENV PHP_CPPFLAGS="$PHP_CPPFLAGS -std=c++11"

RUN docker-php-ext-install pdo_mysql \
    && docker-php-ext-install opcache \
    && apt-get install libicu-dev -y \
    && docker-php-ext-configure intl \
    && docker-php-ext-install intl \
    && apt-get remove libicu-dev icu-devtools -y
RUN { \
        echo 'opcache.memory_consumption=128'; \
        echo 'opcache.interned_strings_buffer=8'; \
        echo 'opcache.max_accelerated_files=4000'; \
        echo 'opcache.revalidate_freq=2'; \
        echo 'opcache.fast_shutdown=1'; \
        echo 'opcache.enable_cli=1'; \
    } > /usr/local/etc/php/conf.d/php-opocache-cfg.ini

COPY nginx-site.conf /etc/nginx/sites-enabled/default
COPY entrypoint.sh /etc/entrypoint.sh

COPY --chown=www-data:www-data . /var/www/mysite

WORKDIR /var/www/mysite

EXPOSE 80 443

ENTRYPOINT ["/etc/entrypoint.sh"]

The nginx-site.conf file contain your nginx http host configuration. The example below is for Symfony app:

server {
    root    /var/www/mysite/web;

    include /etc/nginx/default.d/*.conf;

    index app.php index.php index.html index.htm;

    client_max_body_size 30m;

    location / {
        try_files $uri $uri/ /app.php$is_args$args;
    }

    location ~ [^/]\.php(/|$) {
        fastcgi_split_path_info ^(.+?\.php)(/.*)$;
        # Mitigate https://httpoxy.org/ vulnerabilities
        fastcgi_param HTTP_PROXY "";
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index app.php;
        include fastcgi.conf;
    }
}

The entrypoint.sh will run nginx and php-fpm on container startup (otherwise only php-fpm will be started as the default action of the official PHP image):

#!/usr/bin/env bash
service nginx start
php-fpm

Ofcourse this is not a best way from the best practise prospective but I hope this is the answer for your question.