官方nginx和php docker图像的“Hello World”。 如何?

我想运行一些简单的index.php文件,使用官方的nginx和php docker镜像。 这些是我的先决条件:

  1. Ubuntu的版本是16.04.1 LTS(我的主机)
  2. docker版本是1.12.6
  3. docker工人版本1.9.0

我的主机上的本地目录如下所示:

\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 

我在控制台中看到两个图像都已创build,但是,当我转到localhost:8181时,出现一条错误消息。 这是错误消息的全文:

* 1“/etc/nginx/html/index.php”没有find(2:没有这样的文件或目录),客户端:172.17.0.1,服务器:,请求:“GET / HTTP / 1.1”,主机:“本地主机:8181"

我真的想知道,为什么它在/etc/nginx/html/文件夹中search并忽略root /code; 来自nginx.conf文件的指令。 不过,我编辑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 

正如你所看到的,我只是在networking/卷下更改了一条指令:

 ./code:/code -> ./code:/etc/nginx/html 

现在,当我重新运行docker-compose up --force-recreate并转到localhost:8181我看到另一个错误消息:

FastCGI在stderr中发送:读取来自上游的响应标题“主要脚本未知”,客户端:172.17.0.1,server:,请求:“GET / HTTP / 1.1”,上游:“fastcgi://172.17.0.2:9000”,主机:“localhost:8181”

现在我不知道这是什么意思。 之后,我在nginx.conf中尝试了许多不同的指令组合,但是每次我都以完全相同的错误信息结束失败。 所以,我的问题是如何使它工作? docker-compose.yml和nginx.conf应该如何在ordrer中运行世界上最简单的脚本?

你的PHP容器卷定义中缺less“ – ”。

 php: image: php:7-fpm volumes: ./code:/code 

应该:

 php: image: php:7-fpm volumes: - ./code:/code 

我自己testing了一下,用这个修正它工作得很好:

 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