![](/img/trans.png)
[英]Docker - docker-compose: php-fpm works from image, but don't work from build in Dockerfile
[英]Why php-fpm from official Docker image doesn't work for me?
我嘗試從php:fpm
運行一個新容器php:fpm
:
docker run --name fpmtest -d -p 80:9000 php:fpm
默認情況下,它在其Dockerfile中公開端口9000。
然后我登錄到容器並創建index.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
我想你誤解了那個容器的用途。 沒有Web服務器正在偵聽。
容器的端口9000是Web服務器可以用來與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/
作為一個例子 ,假設我們想要將php-fpm容器與另一個運行nginx Web服務器的容器一起使用 。
首先,使用php文件創建一個目錄,例如:
mkdir content
echo '<?php echo "Hello World!"?>' > content/index.php
然后,創建另一個目錄conf.d
,並在其中創建一個包含以下內容的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並查看結果。
要考慮到:
--link fpmtest:fpmtestdocker
param,以便從nginx容器中看到fpm容器。 然后我們可以添加fastcgi_pass fpmtestdocker:9000;
nginx服務器配置中的config指令。 這是未經測試的,但松散地基於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)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.