为什么从官方的Docker镜像的PHP-FPM不适合我?

我尝试从php:fpm运行一个新的容器php:fpm

docker run --name fpmtest -d -p 80:9000 php:fpm

默认情况下,它在Dockerfile中显示端口9000。

然后,我login到容器并创buildindex.html文件:

 $ docker exec -i -t fpmtest bash root@2fb39dd6a40b:/var/www/html# echo "Hello, World!" > index.html 

在容器内部,我试着用curl来获得这个内容:

 # curl localhost:9000 curl: (56) Recv failure: Connection reset by peer 

在容器外面,我得到另一个错误:

 $ curl localhost curl: (52) Empty reply from server 

我想你误解了那个容器的用途。 没有一个networking服务器在监听。

容器的端口9000是一个networking服务器可以用来与php解释器通信的套接字。

在你链接的git仓库的父文件夹中,有另外一个运行apache容器的文件夹,它似乎在那里和fpm容器一起工作。

我想你的情况你应该这样做:

 docker run -it --rm --name my-apache-php-app -v /PATH/TO/WEB-FILES:/var/www/html php:5.6-apache 

以下是使用php docker镜像的官方文档:

https://registry.hub.docker.com/_/php/


作为一个例子 ,假设我们想用另一个运行nginx web服务器的容器来使用这个php-fpm容器。

首先,用php文件创build一个目录,例如:

 mkdir content echo '<?php echo "Hello World!"?>' > content/index.php 

然后,创build另一个目录conf.d ,并在其中创build一个包含以下内容的default.conf文件:

 server { server_name localhost; root /var/www/html; index index.php index.html index.htm; location / { try_files $uri $uri/ /index.html; } location ~ \.php$ { try_files $uri =404; #fastcgi_pass unix:/var/run/php5-fpm.sock; fastcgi_pass fpmtestdocker:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } } 

请注意fastcgi_pass参数值。 那么,在这种情况下,我们首先运行:

 docker run --name fpmtest -d -p 9000:9000 -v $PWD/content:/var/www/html php:fpm 

接着:

 docker run --name nginxtest -p 80:80 --link fpmtest:fpmtestdocker -v $PWD/content:/var/www/html -v $PWD/conf.d:/etc/nginx/conf.d -d nginx 

就是这样。 我们可以去http:// localhost并查看结果。

考虑到:

  • 这两个容器都需要访问相同的/ var / www / html目录。 他们共享应用程序文件的path。
  • --link fpmtest:fpmtestdocker参数,以便从nginx容器中可以看到fpm容器。 然后我们可以添加fastcgi_pass fpmtestdocker:9000; configuration指令在nginx服务器configuration。

这是未经testing,但松散的基础上,MD5的出色的要点 。 要用nginx来使用这个图片,你的Dockerfile就像这样:

Dockerfile

 FROM nginx:1.7 COPY php-fpm.conf /etc/nginx.conf.d/default.conf 

您复制的nginx.conf示例可能如下所示。

PHP-fpm.conf

 server { listen 80; server_name localhost; root /var/www/html; index index.php; location ~ [^/]\.php(/|$) { fastcgi_split_path_info ^(.+?\.php)(/.*)$; if (!-f $document_root$fastcgi_script_name) { return 404; } include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info; fastcgi_pass fpmtest:9000; fastcgi_index index.php; } } 

注意fastcgi_pass引用你的容器名称(fpmtest)。