“Hello World”与官方的nginx和php码头图像。如何? [英] "Hello World" with official nginx and php docker images. Howto?

查看:264
本文介绍了“Hello World”与官方的nginx和php码头图像。如何?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to run some simple index.php file, using official nginx and php docker images. These are my prerequisites:


  1. ubuntu版本是16.04.1 LTS(我的主机)

  2. docker版本是1.12.6

  3. docker-compose版本1.9.0

我的本​​地目录在我的主机上看起来像这样:

My local directory on my host machine looks like so:

\code
    index.php
docker-compose.yml
nginx.conf

index.php包含一些简单的代码:

index.php contains some simple code:

<?php
    echo phpinfo();
?>

docker-compose.yml包含这些说明(版本1):

docker-compose.yml contains these instructions (version 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):

nginx.conf contains these instructions (version 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

我在控制台中看到这两个图像都是创建的,但是当我转到本地主机时:8181,我收到一条错误消息。这是错误信息的全文:

I see in the console that both images are created, however, when I go to localhost:8181, I get an error message. This is the full text of the error message:


* 1未找到/etc/nginx/html/index.php 2:没有这样的文件或目录),客户端:172.17.0.1,服务器:请求:GET / HTTP / 1.1,主机:localhost:8181

*1 "/etc/nginx/html/index.php" is not found (2: No such file or directory), client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8181"

我真的很奇怪,为什么在 / etc / nginx / html / 文件夹中搜索,忽略 root / code; / code>指令从nginx.conf文件。然而,我编辑docker-compose.yml,现在看起来像这样:

I really wonder, why it searches in /etc/nginx/html/ folder and ignores root /code; instruction from nginx.conf file. Nevertheless, I edit docker-compose.yml, so that it now looks like so:

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

如你所见,我已经改变了一个指令网页/卷:

As you can see, I've changed just one instruction under web/volumes:

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

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

Now, when I rerun docker-compose up --force-recreate and go to localhost:8181 I see another error message:


FastCGI在stderr中发送:主脚本未知,同时从上游读取响应头,客户端:172.17.0.1,server:请求:GET / HTTP / 1.1,上游:fastcgi://172.17.0.2:9000主持人:localhost:8181

FastCGI Sent in stderr: "Primary script unknown" while reading response header from upstream, client: 172.17.0.1, server: , request: "GET / HTTP/1.1" , upstream : "fastcgi://172.17.0.2:9000", host: "localhost:8181"

现在我不知道这一切是什么意思。之后,我在nginx.conf中尝试了许多不同的指令组合,但是每当我完成相同的错误消息时,它都会失败。那么我的问题是如何让它工作? Docker-compose.yml和nginx.conf应该如何在ordrer中看起来像在世界上运行最简单的脚本?

Now I do not know what it all means. After that I tried many different combinations of instructions in nginx.conf, but it each time I ended in failure with the exact same error message. So, my question is how can I make it work? How docker-compose.yml and nginx.conf should look like in ordrer to be able to run the simplest script in the world?

推荐答案

p>您的php容器卷定义中缺少 - 。

You have a missing "-" in your php container volume definition.

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

应该是:

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

我自己测试过,并且通过该修正可以正常工作:

I tested it myself and with that correction it works fine:

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

这篇关于“Hello World”与官方的nginx和php码头图像。如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆