Using nginx in a php container

时间:2017-11-08 22:00:47

标签: php docker nginx

I have this Dockerfile based on php-fpm

FROM php:7.1-fpm

RUN apt-get -y update && apt-get -y upgrade

# Basics
RUN apt-get -y install vim nano git curl cron zsh

# www-data user
RUN usermod -u 1000 www-data

RUN apt-get update \
    && apt-get install -y nginx \
    && apt-get clean \
    && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/* \
    && echo "daemon off;" >> /etc/nginx/nginx.conf

ADD default.conf /etc/nginx/sites-available/default

ADD . /var/www
WORKDIR /var/www

EXPOSE 80 9000
CMD ["nginx"]

I have installed nginx in this image and added configuration: default.conf

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www;
    index index.html index.htm index.php;

    server_name _;

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }
}

This can load very well a html file but I don't know how to load a php file when I'm using a based php image.

I don't find the php socket to use fast cgi mode in this php-fpm image.

With a separetad php-fpm container I'll manage it that way:

location ~ ^/(index)\.php(/|$) {
        fastcgi_pass php-fpm:9000;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param HTTPS off;
    }

But I want to use one single container. How can I do it? How can I change this line?

fastcgi_pass php-fpm:9000;

2 个答案:

答案 0 :(得分:1)

有几种方法,

  1. phpnginx命令放入脚本中,比如run.sh,然后CMD run.sh
  2. 使用像supervisord
  3. 这样的流程管理器

答案 1 :(得分:0)

您可以使用现有的PHP-FPM +NGINX container。或者您可以查看Container's dockerfile它可以提供一些帮助。