“Hello World” with official nginx and php docker images. Howto?

孤人 提交于 2019-12-08 10:20:12

问题


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

  1. ubuntu version is 16.04.1 LTS (my host machine)
  2. docker version is 1.12.6
  3. docker-compose version 1.9.0

My local directory on my host machine looks like so:

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

index.php contains some simple code:

<?php
    echo phpinfo();
?>

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 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;
        }

    }
}

With all this I run the command:

docker-compose up --force-recreate

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" is not found (2: No such file or directory), client: 172.17.0.1, server: , request: "GET / HTTP/1.1", host: "localhost:8181"

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

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

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"

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?


回答1:


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

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

Should be:

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


来源:https://stackoverflow.com/questions/41666793/hello-world-with-official-nginx-and-php-docker-images-howto

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!