我想运行一些简单的index.php文件,使用官方的nginx和php docker镜像。这些是我的先决条件:
我的主机上的本地目录如下所示:
\code
index.php
docker-compose.yml
nginx.conf
index.php包含一些简单的代码:
<?php
echo phpinfo();
?>
docker-compose.yml包含这些说明(版本1):
web:
image: nginx:latest
ports:
- "8181:80"
volumes:
- ./code:/code
- ./nginx.conf:/etc/nginx/nginx.conf
links:
- php
php:
image: php:7-fpm
volumes:
./code:/code
nginx.conf包含这些说明(版本1):
worker_processes 1
events { worker_connections 1024; }
http {
sendfile on;
server {
listen 80;
index index.php index.html;
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;
}
}
}
通过这一切,我运行命令:
docker-compose up --force-recreate
我在控制台中看到两个图像都已创建,但是,当我转到localhost:8181时,收到错误消息。这是错误消息的全文:
找不到* 1“/etc/nginx/html/index.php”(2:没有这样的文件或目录),客户端:172.17.0.1,server :, request:“GET / HTTP / 1.1”,host :“localhost:8181”
我真的很想知道,为什么它会在/etc/nginx/html/
文件夹中搜索并忽略来自nginx.conf文件的root /code;
指令。不过,我编辑了docker-compose.yml,所以它现在看起来像这样:
web:
image: nginx:latest
ports:
- "8181:80"
volumes:
- ./code:/etc/nginx/html
- ./nginx.conf:/etc/nginx/nginx.conf
links:
- php
php:
image: php:7-fpm
volumes:
./code:/code
正如您所看到的,我在web /卷下只更改了一条指令:
./code:/code -> ./code:/etc/nginx/html
现在,当我重新运行docker-compose up --force-recreate
并转到localhost:8181时,我看到另一条错误消息:
FastCGI在stderr中发送:“主脚本未知”,同时从上游读取响应头,客户端:172.17.0.1,server :, request:“GET / HTTP / 1.1”,上游:“fastcgi://172.17.0.2: 9000“,主持人:”localhost:8181“
现在我不知道这一切意味着什么。之后我在nginx.conf中尝试了许多不同的指令组合,但每次我都以完全相同的错误消息结束失败。所以,我的问题是我怎样才能让它发挥作用? docker-compose.yml和nginx.conf在ordrer中应该如何能够运行世界上最简单的脚本?
答案 0 :(得分:1)
你遗失了#34; - &#34;在你的php容器卷定义中。
php:
image: php:7-fpm
volumes:
./code:/code
应该是:
php:
image: php:7-fpm
volumes:
- ./code:/code
我自己进行了测试,并且通过校正可以正常工作:
Starting test_php_1
Starting test_web_1
Attaching to test_php_1, test_web_1
php_1 | [19-Jan-2017 14:41:09] NOTICE: fpm is running, pid 1
php_1 | [19-Jan-2017 14:41:09] NOTICE: ready to handle connections
web_1 | 172.17.0.1 - - [19/Jan/2017:14:41:39 +0000] "GET / HTTP/1.1" 200 82883 "-" "curl/7.35.0"
php_1 | 172.17.0.3 - 19/Jan/2017:14:41:39 +0000 "GET /index.php" 200